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  * 
39  * 
40  * 
41  * @constructor
42  * Create a new Input
43  * @param {Object} config The config object
44  */
45
46 Roo.bootstrap.Input = function(config){
47     
48     Roo.bootstrap.Input.superclass.constructor.call(this, config);
49     
50     this.addEvents({
51         /**
52          * @event focus
53          * Fires when this field receives input focus.
54          * @param {Roo.form.Field} this
55          */
56         focus : true,
57         /**
58          * @event blur
59          * Fires when this field loses input focus.
60          * @param {Roo.form.Field} this
61          */
62         blur : true,
63         /**
64          * @event specialkey
65          * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.  You can check
66          * {@link Roo.EventObject#getKey} to determine which key was pressed.
67          * @param {Roo.form.Field} this
68          * @param {Roo.EventObject} e The event object
69          */
70         specialkey : true,
71         /**
72          * @event change
73          * Fires just before the field blurs if the field value has changed.
74          * @param {Roo.form.Field} this
75          * @param {Mixed} newValue The new value
76          * @param {Mixed} oldValue The original value
77          */
78         change : true,
79         /**
80          * @event invalid
81          * Fires after the field has been marked as invalid.
82          * @param {Roo.form.Field} this
83          * @param {String} msg The validation message
84          */
85         invalid : true,
86         /**
87          * @event valid
88          * Fires after the field has been validated with no errors.
89          * @param {Roo.form.Field} this
90          */
91         valid : true,
92          /**
93          * @event keyup
94          * Fires after the key up
95          * @param {Roo.form.Field} this
96          * @param {Roo.EventObject}  e The event Object
97          */
98         keyup : true
99     });
100 };
101
102 Roo.extend(Roo.bootstrap.Input, Roo.bootstrap.Component,  {
103      /**
104      * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
105       automatic validation (defaults to "keyup").
106      */
107     validationEvent : "keyup",
108      /**
109      * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
110      */
111     validateOnBlur : true,
112     /**
113      * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
114      */
115     validationDelay : 250,
116      /**
117      * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
118      */
119     focusClass : "x-form-focus",  // not needed???
120     
121        
122     /**
123      * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
124      */
125     invalidClass : "has-warning",
126     
127     /**
128      * @cfg {String} validClass The CSS class to use when marking a field valid (defaults to "x-form-invalid")
129      */
130     validClass : "has-success",
131     
132     /**
133      * @cfg {Boolean} hasFeedback (true|false) default true
134      */
135     hasFeedback : true,
136     
137     /**
138      * @cfg {String} invalidFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
139      */
140     invalidFeedbackClass : "glyphicon-warning-sign",
141     
142     /**
143      * @cfg {String} validFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
144      */
145     validFeedbackClass : "glyphicon-ok",
146     
147     /**
148      * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives input focus (defaults to false)
149      */
150     selectOnFocus : false,
151     
152      /**
153      * @cfg {String} maskRe An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null)
154      */
155     maskRe : null,
156        /**
157      * @cfg {String} vtype A validation type name as defined in {@link Roo.form.VTypes} (defaults to null)
158      */
159     vtype : null,
160     
161       /**
162      * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
163      */
164     disableKeyFilter : false,
165     
166        /**
167      * @cfg {Boolean} disabled True to disable the field (defaults to false).
168      */
169     disabled : false,
170      /**
171      * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
172      */
173     allowBlank : true,
174     /**
175      * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
176      */
177     blankText : "Please complete this mandatory field",
178     
179      /**
180      * @cfg {Number} minLength Minimum input field length required (defaults to 0)
181      */
182     minLength : 0,
183     /**
184      * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
185      */
186     maxLength : Number.MAX_VALUE,
187     /**
188      * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to "The minimum length for this field is {minLength}")
189      */
190     minLengthText : "The minimum length for this field is {0}",
191     /**
192      * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to "The maximum length for this field is {maxLength}")
193      */
194     maxLengthText : "The maximum length for this field is {0}",
195   
196     
197     /**
198      * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
199      * If available, this function will be called only after the basic validators all return true, and will be passed the
200      * current field value and expected to return boolean true if the value is valid or a string error message if invalid.
201      */
202     validator : null,
203     /**
204      * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
205      * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
206      * current field value.  If the test fails, the field will be marked invalid using {@link #regexText}.
207      */
208     regex : null,
209     /**
210      * @cfg {String} regexText -- Depricated - use Invalid Text
211      */
212     regexText : "",
213     
214     /**
215      * @cfg {String} invalidText The error text to display if {@link #validator} test fails during validation (defaults to "")
216      */
217     invalidText : "",
218     
219     
220     
221     autocomplete: false,
222     
223     
224     fieldLabel : '',
225     inputType : 'text',
226     
227     name : false,
228     placeholder: false,
229     before : false,
230     after : false,
231     size : false,
232     hasFocus : false,
233     preventMark: false,
234     isFormField : true,
235     value : '',
236     labelWidth : 2,
237     labelAlign : false,
238     readOnly : false,
239     align : false,
240     formatedValue : false,
241     forceFeedback : false,
242     
243     indicatorpos : 'left',
244     
245     labellg : 0,
246     labelmd : 0,
247     labelsm : 0,
248     labelxs : 0,
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.align){
286             input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
287         }
288         
289         if(this.maxLength && this.maxLength != Number.MAX_VALUE){
290             input.maxLength = this.maxLength;
291         }
292         
293         if (this.disabled) {
294             input.disabled=true;
295         }
296         
297         if (this.readOnly) {
298             input.readonly=true;
299         }
300         
301         if (this.name) {
302             input.name = this.name;
303         }
304         
305         if (this.size) {
306             input.cls += ' input-' + this.size;
307         }
308         
309         var settings=this;
310         ['xs','sm','md','lg'].map(function(size){
311             if (settings[size]) {
312                 cfg.cls += ' col-' + size + '-' + settings[size];
313             }
314         });
315         
316         var inputblock = input;
317         
318         var feedback = {
319             tag: 'span',
320             cls: 'glyphicon form-control-feedback'
321         };
322             
323         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
324             
325             inputblock = {
326                 cls : 'has-feedback',
327                 cn :  [
328                     input,
329                     feedback
330                 ] 
331             };  
332         }
333         
334         if (this.before || this.after) {
335             
336             inputblock = {
337                 cls : 'input-group',
338                 cn :  [] 
339             };
340             
341             if (this.before && typeof(this.before) == 'string') {
342                 
343                 inputblock.cn.push({
344                     tag :'span',
345                     cls : 'roo-input-before input-group-addon',
346                     html : this.before
347                 });
348             }
349             if (this.before && typeof(this.before) == 'object') {
350                 this.before = Roo.factory(this.before);
351                 
352                 inputblock.cn.push({
353                     tag :'span',
354                     cls : 'roo-input-before input-group-' +
355                         (this.before.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
356                 });
357             }
358             
359             inputblock.cn.push(input);
360             
361             if (this.after && typeof(this.after) == 'string') {
362                 inputblock.cn.push({
363                     tag :'span',
364                     cls : 'roo-input-after input-group-addon',
365                     html : this.after
366                 });
367             }
368             if (this.after && typeof(this.after) == 'object') {
369                 this.after = Roo.factory(this.after);
370                 
371                 inputblock.cn.push({
372                     tag :'span',
373                     cls : 'roo-input-after input-group-' +
374                         (this.after.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
375                 });
376             }
377             
378             if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
379                 inputblock.cls += ' has-feedback';
380                 inputblock.cn.push(feedback);
381             }
382         };
383         
384         if (align ==='left' && this.fieldLabel.length) {
385             
386             cfg.cls += ' roo-form-group-label-left';
387             
388             cfg.cn = [
389                 {
390                     tag : 'i',
391                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
392                     tooltip : 'This field is required'
393                 },
394                 {
395                     tag: 'label',
396                     'for' :  id,
397                     cls : 'control-label',
398                     html : this.fieldLabel
399
400                 },
401                 {
402                     cls : "", 
403                     cn: [
404                         inputblock
405                     ]
406                 }
407             ];
408             
409             var labelCfg = cfg.cn[1];
410             var contentCfg = cfg.cn[2];
411             
412             if(this.indicatorpos == 'right'){
413                 cfg.cn = [
414                     {
415                         tag: 'label',
416                         'for' :  id,
417                         cls : 'control-label',
418                         cn : [
419                             {
420                                 tag : 'span',
421                                 html : this.fieldLabel
422                             },
423                             {
424                                 tag : 'i',
425                                 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
426                                 tooltip : 'This field is required'
427                             }
428                         ]
429                     },
430                     {
431                         cls : "",
432                         cn: [
433                             inputblock
434                         ]
435                     }
436
437                 ];
438                 
439                 labelCfg = cfg.cn[0];
440                 contentCfg = cfg.cn[1];
441             
442             }
443             
444             if(this.labelWidth > 12){
445                 labelCfg.style = "width: " + this.labelWidth + 'px';
446             }
447             
448             if(this.labelWidth < 13 && this.labelmd == 0){
449                 this.labelmd = this.labelWidth;
450             }
451             
452             if(this.labellg > 0){
453                 labelCfg.cls += ' col-lg-' + this.labellg;
454                 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
455             }
456             
457             if(this.labelmd > 0){
458                 labelCfg.cls += ' col-md-' + this.labelmd;
459                 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
460             }
461             
462             if(this.labelsm > 0){
463                 labelCfg.cls += ' col-sm-' + this.labelsm;
464                 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
465             }
466             
467             if(this.labelxs > 0){
468                 labelCfg.cls += ' col-xs-' + this.labelxs;
469                 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
470             }
471             
472             
473         } else if ( this.fieldLabel.length) {
474                 
475             cfg.cn = [
476                 {
477                     tag : 'i',
478                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
479                     tooltip : 'This field is required'
480                 },
481                 {
482                     tag: 'label',
483                    //cls : 'input-group-addon',
484                     html : this.fieldLabel
485
486                 },
487
488                inputblock
489
490            ];
491            
492            if(this.indicatorpos == 'right'){
493                 
494                 cfg.cn = [
495                     {
496                         tag: 'label',
497                        //cls : 'input-group-addon',
498                         html : this.fieldLabel
499
500                     },
501                     {
502                         tag : 'i',
503                         cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
504                         tooltip : 'This field is required'
505                     },
506
507                    inputblock
508
509                ];
510
511             }
512
513         } else {
514             
515             cfg.cn = [
516
517                     inputblock
518
519             ];
520                 
521                 
522         };
523         
524         if (this.parentType === 'Navbar' &&  this.parent().bar) {
525            cfg.cls += ' navbar-form';
526         }
527         
528         if (this.parentType === 'NavGroup') {
529            cfg.cls += ' navbar-form';
530            cfg.tag = 'li';
531         }
532         
533         return cfg;
534         
535     },
536     /**
537      * return the real input element.
538      */
539     inputEl: function ()
540     {
541         return this.el.select('input.form-control',true).first();
542     },
543     
544     tooltipEl : function()
545     {
546         return this.inputEl();
547     },
548     
549     indicatorEl : function()
550     {
551         var indicator = this.el.select('i.roo-required-indicator',true).first();
552         
553         if(!indicator){
554             return false;
555         }
556         
557         return indicator;
558         
559     },
560     
561     setDisabled : function(v)
562     {
563         var i  = this.inputEl().dom;
564         if (!v) {
565             i.removeAttribute('disabled');
566             return;
567             
568         }
569         i.setAttribute('disabled','true');
570     },
571     initEvents : function()
572     {
573           
574         this.inputEl().on("keydown" , this.fireKey,  this);
575         this.inputEl().on("focus", this.onFocus,  this);
576         this.inputEl().on("blur", this.onBlur,  this);
577         
578         this.inputEl().relayEvent('keyup', this);
579         
580         this.indicator = this.indicatorEl();
581         
582         if(this.indicator){
583             this.indicator.addClass('invisible');
584             
585         }
586  
587         // reference to original value for reset
588         this.originalValue = this.getValue();
589         //Roo.form.TextField.superclass.initEvents.call(this);
590         if(this.validationEvent == 'keyup'){
591             this.validationTask = new Roo.util.DelayedTask(this.validate, this);
592             this.inputEl().on('keyup', this.filterValidation, this);
593         }
594         else if(this.validationEvent !== false){
595             this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
596         }
597         
598         if(this.selectOnFocus){
599             this.on("focus", this.preFocus, this);
600             
601         }
602         if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
603             this.inputEl().on("keypress", this.filterKeys, this);
604         } else {
605             this.inputEl().relayEvent('keypress', this);
606         }
607        /* if(this.grow){
608             this.el.on("keyup", this.onKeyUp,  this, {buffer:50});
609             this.el.on("click", this.autoSize,  this);
610         }
611         */
612         if(this.inputEl().is('input[type=password]') && Roo.isSafari){
613             this.inputEl().on('keydown', this.SafariOnKeyDown, this);
614         }
615         
616         if (typeof(this.before) == 'object') {
617             this.before.render(this.el.select('.roo-input-before',true).first());
618         }
619         if (typeof(this.after) == 'object') {
620             this.after.render(this.el.select('.roo-input-after',true).first());
621         }
622         
623         
624     },
625     filterValidation : function(e){
626         if(!e.isNavKeyPress()){
627             this.validationTask.delay(this.validationDelay);
628         }
629     },
630      /**
631      * Validates the field value
632      * @return {Boolean} True if the value is valid, else false
633      */
634     validate : function(){
635         //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
636         if(this.disabled || this.validateValue(this.getRawValue())){
637             this.markValid();
638             return true;
639         }
640         
641         this.markInvalid();
642         return false;
643     },
644     
645     
646     /**
647      * Validates a value according to the field's validation rules and marks the field as invalid
648      * if the validation fails
649      * @param {Mixed} value The value to validate
650      * @return {Boolean} True if the value is valid, else false
651      */
652     validateValue : function(value){
653         if(value.length < 1)  { // if it's blank
654             if(this.allowBlank){
655                 return true;
656             }            
657             return this.inputEl().hasClass('hide') ? true : false;
658         }
659         
660         if(value.length < this.minLength){
661             return false;
662         }
663         if(value.length > this.maxLength){
664             return false;
665         }
666         if(this.vtype){
667             var vt = Roo.form.VTypes;
668             if(!vt[this.vtype](value, this)){
669                 return false;
670             }
671         }
672         if(typeof this.validator == "function"){
673             var msg = this.validator(value);
674             if(msg !== true){
675                 return false;
676             }
677             if (typeof(msg) == 'string') {
678                 this.invalidText = msg;
679             }
680         }
681         
682         if(this.regex && !this.regex.test(value)){
683             return false;
684         }
685         
686         return true;
687     },
688
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.disabled){
884             return;
885         }
886         
887         if(this.allowBlank && !this.getRawValue().length){
888             return;
889         }
890         
891         if(this.indicator){
892             this.indicator.removeClass('visible');
893             this.indicator.addClass('invisible');
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         
1020         
1021         if(this.indicator){
1022             var ar = this.el.select('label > span',true);
1023             
1024             if (ar.elements.length) {
1025                 this.el.select('label > span',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1026                 this.fieldLabel = v;
1027                 return;
1028             }
1029             
1030             var br = this.el.select('label',true);
1031             
1032             if(br.elements.length) {
1033                 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1034                 this.fieldLabel = v;
1035                 return;
1036             }
1037             
1038             Roo.log('Cannot Found any of label > span || label in input');
1039         }
1040         
1041         
1042     }
1043 });
1044
1045