Roo/bootstrap/Input.js
[roojs1] / Roo / bootstrap / Input.js
1 /*
2  * - LGPL
3  *
4  * Input
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.Input
10  * @extends Roo.bootstrap.Component
11  * Bootstrap Input class
12  * @cfg {Boolean} disabled is it disabled
13  * @cfg {String} inputType button | checkbox | email | file | hidden | image | number | password | radio | range | reset | search | submit | text
14  * @cfg {String} name name of the input
15  * @cfg {string} fieldLabel - the label associated
16  * @cfg {string} placeholder - placeholder to put in text.
17  * @cfg {string}  before - input group add on before
18  * @cfg {string} after - input group add on after
19  * @cfg {string} size - (lg|sm) or leave empty..
20  * @cfg {Number} xs colspan out of 12 for mobile-sized screens
21  * @cfg {Number} sm colspan out of 12 for tablet-sized screens
22  * @cfg {Number} md colspan out of 12 for computer-sized screens
23  * @cfg {Number} lg colspan out of 12 for large computer-sized screens
24  * @cfg {string} value default value of the input
25  * @cfg {Number} labelWidth set the width of label 
26  * @cfg {Number} labellg set the width of label (1-12)
27  * @cfg {Number} labelmd set the width of label (1-12)
28  * @cfg {Number} labelsm set the width of label (1-12)
29  * @cfg {Number} labelxs set the width of label (1-12)
30  * @cfg {String} labelAlign (top|left)
31  * @cfg {Boolean} readOnly Specifies that the field should be read-only
32  * @cfg {String} autocomplete - default is new-password see: https://developers.google.com/web/fundamentals/input/form/label-and-name-inputs?hl=en
33  * @cfg {String} indicatorpos (left|right) default left
34  * @cfg {String} capture (user|camera) use for file input only. (default empty)
35
36  * @cfg {String} align (left|center|right) Default left
37  * @cfg {Boolean} forceFeedback (true|false) Default false
38  * 
39  * @constructor
40  * Create a new Input
41  * @param {Object} config The config object
42  */
43
44 Roo.bootstrap.Input = function(config){
45     
46     Roo.bootstrap.Input.superclass.constructor.call(this, config);
47     
48     this.addEvents({
49         /**
50          * @event focus
51          * Fires when this field receives input focus.
52          * @param {Roo.form.Field} this
53          */
54         focus : true,
55         /**
56          * @event blur
57          * Fires when this field loses input focus.
58          * @param {Roo.form.Field} this
59          */
60         blur : true,
61         /**
62          * @event specialkey
63          * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.  You can check
64          * {@link Roo.EventObject#getKey} to determine which key was pressed.
65          * @param {Roo.form.Field} this
66          * @param {Roo.EventObject} e The event object
67          */
68         specialkey : true,
69         /**
70          * @event change
71          * Fires just before the field blurs if the field value has changed.
72          * @param {Roo.form.Field} this
73          * @param {Mixed} newValue The new value
74          * @param {Mixed} oldValue The original value
75          */
76         change : true,
77         /**
78          * @event invalid
79          * Fires after the field has been marked as invalid.
80          * @param {Roo.form.Field} this
81          * @param {String} msg The validation message
82          */
83         invalid : true,
84         /**
85          * @event valid
86          * Fires after the field has been validated with no errors.
87          * @param {Roo.form.Field} this
88          */
89         valid : true,
90          /**
91          * @event keyup
92          * Fires after the key up
93          * @param {Roo.form.Field} this
94          * @param {Roo.EventObject}  e The event Object
95          */
96         keyup : true
97     });
98 };
99
100 Roo.extend(Roo.bootstrap.Input, Roo.bootstrap.Component,  {
101      /**
102      * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
103       automatic validation (defaults to "keyup").
104      */
105     validationEvent : "keyup",
106      /**
107      * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
108      */
109     validateOnBlur : true,
110     /**
111      * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
112      */
113     validationDelay : 250,
114      /**
115      * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
116      */
117     focusClass : "x-form-focus",  // not needed???
118     
119        
120     /**
121      * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
122      */
123     invalidClass : "has-warning",
124     
125     /**
126      * @cfg {String} validClass The CSS class to use when marking a field valid (defaults to "x-form-invalid")
127      */
128     validClass : "has-success",
129     
130     /**
131      * @cfg {Boolean} hasFeedback (true|false) default true
132      */
133     hasFeedback : true,
134     
135     /**
136      * @cfg {String} invalidFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
137      */
138     invalidFeedbackClass : "glyphicon-warning-sign",
139     
140     /**
141      * @cfg {String} validFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
142      */
143     validFeedbackClass : "glyphicon-ok",
144     
145     /**
146      * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives input focus (defaults to false)
147      */
148     selectOnFocus : false,
149     
150      /**
151      * @cfg {String} maskRe An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null)
152      */
153     maskRe : null,
154        /**
155      * @cfg {String} vtype A validation type name as defined in {@link Roo.form.VTypes} (defaults to null)
156      */
157     vtype : null,
158     
159       /**
160      * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
161      */
162     disableKeyFilter : false,
163     
164        /**
165      * @cfg {Boolean} disabled True to disable the field (defaults to false).
166      */
167     disabled : false,
168      /**
169      * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
170      */
171     allowBlank : true,
172     /**
173      * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
174      */
175     blankText : "Please complete this mandatory field",
176     
177      /**
178      * @cfg {Number} minLength Minimum input field length required (defaults to 0)
179      */
180     minLength : 0,
181     /**
182      * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
183      */
184     maxLength : Number.MAX_VALUE,
185     /**
186      * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to "The minimum length for this field is {minLength}")
187      */
188     minLengthText : "The minimum length for this field is {0}",
189     /**
190      * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to "The maximum length for this field is {maxLength}")
191      */
192     maxLengthText : "The maximum length for this field is {0}",
193   
194     
195     /**
196      * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
197      * If available, this function will be called only after the basic validators all return true, and will be passed the
198      * current field value and expected to return boolean true if the value is valid or a string error message if invalid.
199      */
200     validator : null,
201     /**
202      * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
203      * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
204      * current field value.  If the test fails, the field will be marked invalid using {@link #regexText}.
205      */
206     regex : null,
207     /**
208      * @cfg {String} regexText -- Depricated - use Invalid Text
209      */
210     regexText : "",
211     
212     /**
213      * @cfg {String} invalidText The error text to display if {@link #validator} test fails during validation (defaults to "")
214      */
215     invalidText : "",
216     
217     
218     
219     autocomplete: false,
220     
221     
222     fieldLabel : '',
223     inputType : 'text',
224     
225     name : false,
226     placeholder: false,
227     before : false,
228     after : false,
229     size : false,
230     hasFocus : false,
231     preventMark: false,
232     isFormField : true,
233     value : '',
234     labelWidth : 2,
235     labelAlign : false,
236     readOnly : false,
237     align : false,
238     formatedValue : false,
239     forceFeedback : false,
240     
241     indicatorpos : 'left',
242     
243     labellg : 0,
244     labelmd : 0,
245     labelsm : 0,
246     labelxs : 0,
247     
248     capture : '',
249     
250     parentLabelAlign : function()
251     {
252         var parent = this;
253         while (parent.parent()) {
254             parent = parent.parent();
255             if (typeof(parent.labelAlign) !='undefined') {
256                 return parent.labelAlign;
257             }
258         }
259         return 'left';
260         
261     },
262     
263     getAutoCreate : function()
264     {
265         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
266         
267         var id = Roo.id();
268         
269         var cfg = {};
270         
271         if(this.inputType != 'hidden'){
272             cfg.cls = 'form-group' //input-group
273         }
274         
275         var input =  {
276             tag: 'input',
277             id : id,
278             type : this.inputType,
279             value : this.value,
280             cls : 'form-control',
281             placeholder : this.placeholder || '',
282             autocomplete : this.autocomplete || 'new-password'
283         };
284         
285         if(this.capture.length){
286             input.capture = this.capture;
287         }
288         
289         if(this.align){
290             input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
291         }
292         
293         if(this.maxLength && this.maxLength != Number.MAX_VALUE){
294             input.maxLength = this.maxLength;
295         }
296         
297         if (this.disabled) {
298             input.disabled=true;
299         }
300         
301         if (this.readOnly) {
302             input.readonly=true;
303         }
304         
305         if (this.name) {
306             input.name = this.name;
307         }
308         
309         if (this.size) {
310             input.cls += ' input-' + this.size;
311         }
312         
313         var settings=this;
314         ['xs','sm','md','lg'].map(function(size){
315             if (settings[size]) {
316                 cfg.cls += ' col-' + size + '-' + settings[size];
317             }
318         });
319         
320         var inputblock = input;
321         
322         var feedback = {
323             tag: 'span',
324             cls: 'glyphicon form-control-feedback'
325         };
326             
327         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
328             
329             inputblock = {
330                 cls : 'has-feedback',
331                 cn :  [
332                     input,
333                     feedback
334                 ] 
335             };  
336         }
337         
338         if (this.before || this.after) {
339             
340             inputblock = {
341                 cls : 'input-group',
342                 cn :  [] 
343             };
344             
345             if (this.before && typeof(this.before) == 'string') {
346                 
347                 inputblock.cn.push({
348                     tag :'span',
349                     cls : 'roo-input-before input-group-addon',
350                     html : this.before
351                 });
352             }
353             if (this.before && typeof(this.before) == 'object') {
354                 this.before = Roo.factory(this.before);
355                 
356                 inputblock.cn.push({
357                     tag :'span',
358                     cls : 'roo-input-before input-group-' +
359                         (this.before.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
360                 });
361             }
362             
363             inputblock.cn.push(input);
364             
365             if (this.after && typeof(this.after) == 'string') {
366                 inputblock.cn.push({
367                     tag :'span',
368                     cls : 'roo-input-after input-group-addon',
369                     html : this.after
370                 });
371             }
372             if (this.after && typeof(this.after) == 'object') {
373                 this.after = Roo.factory(this.after);
374                 
375                 inputblock.cn.push({
376                     tag :'span',
377                     cls : 'roo-input-after input-group-' +
378                         (this.after.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
379                 });
380             }
381             
382             if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
383                 inputblock.cls += ' has-feedback';
384                 inputblock.cn.push(feedback);
385             }
386         };
387         
388         if (align ==='left' && this.fieldLabel.length) {
389             
390             cfg.cls += ' roo-form-group-label-left';
391             
392             cfg.cn = [
393                 {
394                     tag : 'i',
395                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
396                     tooltip : 'This field is required'
397                 },
398                 {
399                     tag: 'label',
400                     'for' :  id,
401                     cls : 'control-label',
402                     html : this.fieldLabel
403
404                 },
405                 {
406                     cls : "", 
407                     cn: [
408                         inputblock
409                     ]
410                 }
411             ];
412             
413             var labelCfg = cfg.cn[1];
414             var contentCfg = cfg.cn[2];
415             
416             if(this.indicatorpos == 'right'){
417                 cfg.cn = [
418                     {
419                         tag: 'label',
420                         'for' :  id,
421                         cls : 'control-label',
422                         cn : [
423                             {
424                                 tag : 'span',
425                                 html : this.fieldLabel
426                             },
427                             {
428                                 tag : 'i',
429                                 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
430                                 tooltip : 'This field is required'
431                             }
432                         ]
433                     },
434                     {
435                         cls : "",
436                         cn: [
437                             inputblock
438                         ]
439                     }
440
441                 ];
442                 
443                 labelCfg = cfg.cn[0];
444                 contentCfg = cfg.cn[1];
445             
446             }
447             
448             if(this.labelWidth > 12){
449                 labelCfg.style = "width: " + this.labelWidth + 'px';
450             }
451             
452             if(this.labelWidth < 13 && this.labelmd == 0){
453                 this.labelmd = this.labelWidth;
454             }
455             
456             if(this.labellg > 0){
457                 labelCfg.cls += ' col-lg-' + this.labellg;
458                 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
459             }
460             
461             if(this.labelmd > 0){
462                 labelCfg.cls += ' col-md-' + this.labelmd;
463                 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
464             }
465             
466             if(this.labelsm > 0){
467                 labelCfg.cls += ' col-sm-' + this.labelsm;
468                 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
469             }
470             
471             if(this.labelxs > 0){
472                 labelCfg.cls += ' col-xs-' + this.labelxs;
473                 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
474             }
475             
476             
477         } else if ( this.fieldLabel.length) {
478                 
479             cfg.cn = [
480                 {
481                     tag : 'i',
482                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
483                     tooltip : 'This field is required'
484                 },
485                 {
486                     tag: 'label',
487                    //cls : 'input-group-addon',
488                     html : this.fieldLabel
489
490                 },
491
492                inputblock
493
494            ];
495            
496            if(this.indicatorpos == 'right'){
497                 
498                 cfg.cn = [
499                     {
500                         tag: 'label',
501                        //cls : 'input-group-addon',
502                         html : this.fieldLabel
503
504                     },
505                     {
506                         tag : 'i',
507                         cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
508                         tooltip : 'This field is required'
509                     },
510
511                    inputblock
512
513                ];
514
515             }
516
517         } else {
518             
519             cfg.cn = [
520
521                     inputblock
522
523             ];
524                 
525                 
526         };
527         
528         if (this.parentType === 'Navbar' &&  this.parent().bar) {
529            cfg.cls += ' navbar-form';
530         }
531         
532         if (this.parentType === 'NavGroup') {
533            cfg.cls += ' navbar-form';
534            cfg.tag = 'li';
535         }
536         
537         return cfg;
538         
539     },
540     /**
541      * return the real input element.
542      */
543     inputEl: function ()
544     {
545         return this.el.select('input.form-control',true).first();
546     },
547     
548     tooltipEl : function()
549     {
550         return this.inputEl();
551     },
552     
553     indicatorEl : function()
554     {
555         var indicator = this.el.select('i.roo-required-indicator',true).first();
556         
557         if(!indicator){
558             return false;
559         }
560         
561         return indicator;
562         
563     },
564     
565     setDisabled : function(v)
566     {
567         var i  = this.inputEl().dom;
568         if (!v) {
569             i.removeAttribute('disabled');
570             return;
571             
572         }
573         i.setAttribute('disabled','true');
574     },
575     initEvents : function()
576     {
577           
578         this.inputEl().on("keydown" , this.fireKey,  this);
579         this.inputEl().on("focus", this.onFocus,  this);
580         this.inputEl().on("blur", this.onBlur,  this);
581         
582         this.inputEl().relayEvent('keyup', this);
583         
584         this.indicator = this.indicatorEl();
585         
586         if(this.indicator){
587             this.indicator.addClass('invisible');
588         }
589  
590         // reference to original value for reset
591         this.originalValue = this.getValue();
592         //Roo.form.TextField.superclass.initEvents.call(this);
593         if(this.validationEvent == 'keyup'){
594             this.validationTask = new Roo.util.DelayedTask(this.validate, this);
595             this.inputEl().on('keyup', this.filterValidation, this);
596         }
597         else if(this.validationEvent !== false){
598             this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
599         }
600         
601         if(this.selectOnFocus){
602             this.on("focus", this.preFocus, this);
603             
604         }
605         if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
606             this.inputEl().on("keypress", this.filterKeys, this);
607         } else {
608             this.inputEl().relayEvent('keypress', this);
609         }
610        /* if(this.grow){
611             this.el.on("keyup", this.onKeyUp,  this, {buffer:50});
612             this.el.on("click", this.autoSize,  this);
613         }
614         */
615         if(this.inputEl().is('input[type=password]') && Roo.isSafari){
616             this.inputEl().on('keydown', this.SafariOnKeyDown, this);
617         }
618         
619         if (typeof(this.before) == 'object') {
620             this.before.render(this.el.select('.roo-input-before',true).first());
621         }
622         if (typeof(this.after) == 'object') {
623             this.after.render(this.el.select('.roo-input-after',true).first());
624         }
625         
626         this.inputEl().on('change', this.onChange, this);
627         
628     },
629     filterValidation : function(e){
630         if(!e.isNavKeyPress()){
631             this.validationTask.delay(this.validationDelay);
632         }
633     },
634      /**
635      * Validates the field value
636      * @return {Boolean} True if the value is valid, else false
637      */
638     validate : function(){
639         //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
640         if(this.disabled || this.validateValue(this.getRawValue())){
641             this.markValid();
642             return true;
643         }
644         
645         this.markInvalid();
646         return false;
647     },
648     
649     
650     /**
651      * Validates a value according to the field's validation rules and marks the field as invalid
652      * if the validation fails
653      * @param {Mixed} value The value to validate
654      * @return {Boolean} True if the value is valid, else false
655      */
656     validateValue : function(value)
657     {
658         if(this.getVisibilityEl().hasClass('hidden')){
659             return true;
660         }
661         
662         if(value.length < 1)  { // if it's blank
663             if(this.allowBlank){
664                 return true;
665             }
666             return false;
667         }
668         
669         if(value.length < this.minLength){
670             return false;
671         }
672         if(value.length > this.maxLength){
673             return false;
674         }
675         if(this.vtype){
676             var vt = Roo.form.VTypes;
677             if(!vt[this.vtype](value, this)){
678                 return false;
679             }
680         }
681         if(typeof this.validator == "function"){
682             var msg = this.validator(value);
683             if(msg !== true){
684                 return false;
685             }
686             if (typeof(msg) == 'string') {
687                 this.invalidText = msg;
688             }
689         }
690         
691         if(this.regex && !this.regex.test(value)){
692             return false;
693         }
694         
695         return true;
696     },
697     
698      // private
699     fireKey : function(e){
700         //Roo.log('field ' + e.getKey());
701         if(e.isNavKeyPress()){
702             this.fireEvent("specialkey", this, e);
703         }
704     },
705     focus : function (selectText){
706         if(this.rendered){
707             this.inputEl().focus();
708             if(selectText === true){
709                 this.inputEl().dom.select();
710             }
711         }
712         return this;
713     } ,
714     
715     onFocus : function(){
716         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
717            // this.el.addClass(this.focusClass);
718         }
719         if(!this.hasFocus){
720             this.hasFocus = true;
721             this.startValue = this.getValue();
722             this.fireEvent("focus", this);
723         }
724     },
725     
726     beforeBlur : Roo.emptyFn,
727
728     
729     // private
730     onBlur : function(){
731         this.beforeBlur();
732         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
733             //this.el.removeClass(this.focusClass);
734         }
735         this.hasFocus = false;
736         if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
737             this.validate();
738         }
739         var v = this.getValue();
740         if(String(v) !== String(this.startValue)){
741             this.fireEvent('change', this, v, this.startValue);
742         }
743         this.fireEvent("blur", this);
744     },
745     
746     /**
747      * Resets the current field value to the originally loaded value and clears any validation messages
748      */
749     reset : function(){
750         this.setValue(this.originalValue);
751         this.validate();
752     },
753      /**
754      * Returns the name of the field
755      * @return {Mixed} name The name field
756      */
757     getName: function(){
758         return this.name;
759     },
760      /**
761      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
762      * @return {Mixed} value The field value
763      */
764     getValue : function(){
765         
766         var v = this.inputEl().getValue();
767         
768         return v;
769     },
770     /**
771      * Returns the raw data value which may or may not be a valid, defined value.  To return a normalized value see {@link #getValue}.
772      * @return {Mixed} value The field value
773      */
774     getRawValue : function(){
775         var v = this.inputEl().getValue();
776         
777         return v;
778     },
779     
780     /**
781      * Sets the underlying DOM field's value directly, bypassing validation.  To set the value with validation see {@link #setValue}.
782      * @param {Mixed} value The value to set
783      */
784     setRawValue : function(v){
785         return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
786     },
787     
788     selectText : function(start, end){
789         var v = this.getRawValue();
790         if(v.length > 0){
791             start = start === undefined ? 0 : start;
792             end = end === undefined ? v.length : end;
793             var d = this.inputEl().dom;
794             if(d.setSelectionRange){
795                 d.setSelectionRange(start, end);
796             }else if(d.createTextRange){
797                 var range = d.createTextRange();
798                 range.moveStart("character", start);
799                 range.moveEnd("character", v.length-end);
800                 range.select();
801             }
802         }
803     },
804     
805     /**
806      * Sets a data value into the field and validates it.  To set the value directly without validation see {@link #setRawValue}.
807      * @param {Mixed} value The value to set
808      */
809     setValue : function(v){
810         this.value = v;
811         if(this.rendered){
812             this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
813             this.validate();
814         }
815     },
816     
817     /*
818     processValue : function(value){
819         if(this.stripCharsRe){
820             var newValue = value.replace(this.stripCharsRe, '');
821             if(newValue !== value){
822                 this.setRawValue(newValue);
823                 return newValue;
824             }
825         }
826         return value;
827     },
828   */
829     preFocus : function(){
830         
831         if(this.selectOnFocus){
832             this.inputEl().dom.select();
833         }
834     },
835     filterKeys : function(e){
836         var k = e.getKey();
837         if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
838             return;
839         }
840         var c = e.getCharCode(), cc = String.fromCharCode(c);
841         if(Roo.isIE && (e.isSpecialKey() || !cc)){
842             return;
843         }
844         if(!this.maskRe.test(cc)){
845             e.stopEvent();
846         }
847     },
848      /**
849      * Clear any invalid styles/messages for this field
850      */
851     clearInvalid : function(){
852         
853         if(!this.el || this.preventMark){ // not rendered
854             return;
855         }
856         
857      
858         this.el.removeClass(this.invalidClass);
859         
860         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
861             
862             var feedback = this.el.select('.form-control-feedback', true).first();
863             
864             if(feedback){
865                 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
866             }
867             
868         }
869         
870         this.fireEvent('valid', this);
871     },
872     
873      /**
874      * Mark this field as valid
875      */
876     markValid : function()
877     {
878         if(!this.el  || this.preventMark){ // not rendered...
879             return;
880         }
881         
882         this.el.removeClass([this.invalidClass, this.validClass]);
883         
884         var feedback = this.el.select('.form-control-feedback', true).first();
885             
886         if(feedback){
887             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
888         }
889         
890         if(this.indicator){
891             this.indicator.removeClass('visible');
892             this.indicator.addClass('invisible');
893         }
894         
895         if(this.disabled){
896             return;
897         }
898         
899         if(this.allowBlank && !this.getRawValue().length){
900             return;
901         }
902         
903         this.el.addClass(this.validClass);
904         
905         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
906             
907             var feedback = this.el.select('.form-control-feedback', true).first();
908             
909             if(feedback){
910                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
911                 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
912             }
913             
914         }
915         
916         this.fireEvent('valid', this);
917     },
918     
919      /**
920      * Mark this field as invalid
921      * @param {String} msg The validation message
922      */
923     markInvalid : function(msg)
924     {
925         if(!this.el  || this.preventMark){ // not rendered
926             return;
927         }
928         
929         this.el.removeClass([this.invalidClass, this.validClass]);
930         
931         var feedback = this.el.select('.form-control-feedback', true).first();
932             
933         if(feedback){
934             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
935         }
936
937         if(this.disabled){
938             return;
939         }
940         
941         if(this.allowBlank && !this.getRawValue().length){
942             return;
943         }
944         
945         if(this.indicator){
946             this.indicator.removeClass('invisible');
947             this.indicator.addClass('visible');
948         }
949         
950         this.el.addClass(this.invalidClass);
951         
952         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
953             
954             var feedback = this.el.select('.form-control-feedback', true).first();
955             
956             if(feedback){
957                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
958                 
959                 if(this.getValue().length || this.forceFeedback){
960                     this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
961                 }
962                 
963             }
964             
965         }
966         
967         this.fireEvent('invalid', this, msg);
968     },
969     // private
970     SafariOnKeyDown : function(event)
971     {
972         // this is a workaround for a password hang bug on chrome/ webkit.
973         if (this.inputEl().dom.type != 'password') {
974             return;
975         }
976         
977         var isSelectAll = false;
978         
979         if(this.inputEl().dom.selectionEnd > 0){
980             isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
981         }
982         if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
983             event.preventDefault();
984             this.setValue('');
985             return;
986         }
987         
988         if(isSelectAll  && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
989             
990             event.preventDefault();
991             // this is very hacky as keydown always get's upper case.
992             //
993             var cc = String.fromCharCode(event.getCharCode());
994             this.setValue( event.shiftKey ?  cc : cc.toLowerCase());
995             
996         }
997     },
998     adjustWidth : function(tag, w){
999         tag = tag.toLowerCase();
1000         if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
1001             if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
1002                 if(tag == 'input'){
1003                     return w + 2;
1004                 }
1005                 if(tag == 'textarea'){
1006                     return w-2;
1007                 }
1008             }else if(Roo.isOpera){
1009                 if(tag == 'input'){
1010                     return w + 2;
1011                 }
1012                 if(tag == 'textarea'){
1013                     return w-2;
1014                 }
1015             }
1016         }
1017         return w;
1018     },
1019     
1020     setFieldLabel : function(v)
1021     {
1022         if(!this.rendered){
1023             return;
1024         }
1025         
1026         if(this.indicator){
1027             var ar = this.el.select('label > span',true);
1028             
1029             if (ar.elements.length) {
1030                 this.el.select('label > span',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1031                 this.fieldLabel = v;
1032                 return;
1033             }
1034             
1035             var br = this.el.select('label',true);
1036             
1037             if(br.elements.length) {
1038                 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1039                 this.fieldLabel = v;
1040                 return;
1041             }
1042             
1043             Roo.log('Cannot Found any of label > span || label in input');
1044             return;
1045         }
1046         
1047         this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1048         this.fieldLabel = v;
1049         
1050         
1051     }
1052 });
1053
1054