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