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){
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         
627     },
628     filterValidation : function(e){
629         if(!e.isNavKeyPress()){
630             this.validationTask.delay(this.validationDelay);
631         }
632     },
633      /**
634      * Validates the field value
635      * @return {Boolean} True if the value is valid, else false
636      */
637     validate : function(){
638         //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
639         if(this.disabled || this.validateValue(this.getRawValue())){
640             this.markValid();
641             return true;
642         }
643         
644         this.markInvalid();
645         return false;
646     },
647     
648     
649     /**
650      * Validates a value according to the field's validation rules and marks the field as invalid
651      * if the validation fails
652      * @param {Mixed} value The value to validate
653      * @return {Boolean} True if the value is valid, else false
654      */
655     validateValue : function(value)
656     {
657         if(this.getVisibilityEl().hasClass('hidden')){
658             return true;
659         }
660         
661         if(value.length < 1)  { // if it's blank
662             if(this.allowBlank){
663                 return true;
664             }
665             return false;
666         }
667         
668         if(value.length < this.minLength){
669             return false;
670         }
671         if(value.length > this.maxLength){
672             return false;
673         }
674         if(this.vtype){
675             var vt = Roo.form.VTypes;
676             if(!vt[this.vtype](value, this)){
677                 return false;
678             }
679         }
680         if(typeof this.validator == "function"){
681             var msg = this.validator(value);
682             if(msg !== true){
683                 return false;
684             }
685             if (typeof(msg) == 'string') {
686                 this.invalidText = msg;
687             }
688         }
689         
690         if(this.regex && !this.regex.test(value)){
691             return false;
692         }
693         
694         return true;
695     },
696     
697      // private
698     fireKey : function(e){
699         //Roo.log('field ' + e.getKey());
700         if(e.isNavKeyPress()){
701             this.fireEvent("specialkey", this, e);
702         }
703     },
704     focus : function (selectText){
705         if(this.rendered){
706             this.inputEl().focus();
707             if(selectText === true){
708                 this.inputEl().dom.select();
709             }
710         }
711         return this;
712     } ,
713     
714     onFocus : function(){
715         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
716            // this.el.addClass(this.focusClass);
717         }
718         if(!this.hasFocus){
719             this.hasFocus = true;
720             this.startValue = this.getValue();
721             this.fireEvent("focus", this);
722         }
723     },
724     
725     beforeBlur : Roo.emptyFn,
726
727     
728     // private
729     onBlur : function(){
730         this.beforeBlur();
731         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
732             //this.el.removeClass(this.focusClass);
733         }
734         this.hasFocus = false;
735         if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
736             this.validate();
737         }
738         var v = this.getValue();
739         if(String(v) !== String(this.startValue)){
740             this.fireEvent('change', this, v, this.startValue);
741         }
742         this.fireEvent("blur", this);
743     },
744     
745     /**
746      * Resets the current field value to the originally loaded value and clears any validation messages
747      */
748     reset : function(){
749         this.setValue(this.originalValue);
750         this.validate();
751     },
752      /**
753      * Returns the name of the field
754      * @return {Mixed} name The name field
755      */
756     getName: function(){
757         return this.name;
758     },
759      /**
760      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
761      * @return {Mixed} value The field value
762      */
763     getValue : function(){
764         
765         var v = this.inputEl().getValue();
766         
767         return v;
768     },
769     /**
770      * Returns the raw data value which may or may not be a valid, defined value.  To return a normalized value see {@link #getValue}.
771      * @return {Mixed} value The field value
772      */
773     getRawValue : function(){
774         var v = this.inputEl().getValue();
775         
776         return v;
777     },
778     
779     /**
780      * Sets the underlying DOM field's value directly, bypassing validation.  To set the value with validation see {@link #setValue}.
781      * @param {Mixed} value The value to set
782      */
783     setRawValue : function(v){
784         return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
785     },
786     
787     selectText : function(start, end){
788         var v = this.getRawValue();
789         if(v.length > 0){
790             start = start === undefined ? 0 : start;
791             end = end === undefined ? v.length : end;
792             var d = this.inputEl().dom;
793             if(d.setSelectionRange){
794                 d.setSelectionRange(start, end);
795             }else if(d.createTextRange){
796                 var range = d.createTextRange();
797                 range.moveStart("character", start);
798                 range.moveEnd("character", v.length-end);
799                 range.select();
800             }
801         }
802     },
803     
804     /**
805      * Sets a data value into the field and validates it.  To set the value directly without validation see {@link #setRawValue}.
806      * @param {Mixed} value The value to set
807      */
808     setValue : function(v){
809         this.value = v;
810         if(this.rendered){
811             this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
812             this.validate();
813         }
814     },
815     
816     /*
817     processValue : function(value){
818         if(this.stripCharsRe){
819             var newValue = value.replace(this.stripCharsRe, '');
820             if(newValue !== value){
821                 this.setRawValue(newValue);
822                 return newValue;
823             }
824         }
825         return value;
826     },
827   */
828     preFocus : function(){
829         
830         if(this.selectOnFocus){
831             this.inputEl().dom.select();
832         }
833     },
834     filterKeys : function(e){
835         var k = e.getKey();
836         if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
837             return;
838         }
839         var c = e.getCharCode(), cc = String.fromCharCode(c);
840         if(Roo.isIE && (e.isSpecialKey() || !cc)){
841             return;
842         }
843         if(!this.maskRe.test(cc)){
844             e.stopEvent();
845         }
846     },
847      /**
848      * Clear any invalid styles/messages for this field
849      */
850     clearInvalid : function(){
851         
852         if(!this.el || this.preventMark){ // not rendered
853             return;
854         }
855         
856      
857         this.el.removeClass(this.invalidClass);
858         
859         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
860             
861             var feedback = this.el.select('.form-control-feedback', true).first();
862             
863             if(feedback){
864                 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
865             }
866             
867         }
868         
869         this.fireEvent('valid', this);
870     },
871     
872      /**
873      * Mark this field as valid
874      */
875     markValid : function()
876     {
877         if(!this.el  || this.preventMark){ // not rendered...
878             return;
879         }
880         
881         this.el.removeClass([this.invalidClass, this.validClass]);
882         
883         var feedback = this.el.select('.form-control-feedback', true).first();
884             
885         if(feedback){
886             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
887         }
888         
889         if(this.indicator){
890             this.indicator.removeClass('visible');
891             this.indicator.addClass('invisible');
892         }
893         
894         if(this.disabled){
895             return;
896         }
897         
898         if(this.allowBlank && !this.getRawValue().length){
899             return;
900         }
901         
902         this.el.addClass(this.validClass);
903         
904         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
905             
906             var feedback = this.el.select('.form-control-feedback', true).first();
907             
908             if(feedback){
909                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
910                 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
911             }
912             
913         }
914         
915         this.fireEvent('valid', this);
916     },
917     
918      /**
919      * Mark this field as invalid
920      * @param {String} msg The validation message
921      */
922     markInvalid : function(msg)
923     {
924         if(!this.el  || this.preventMark){ // not rendered
925             return;
926         }
927         
928         this.el.removeClass([this.invalidClass, this.validClass]);
929         
930         var feedback = this.el.select('.form-control-feedback', true).first();
931             
932         if(feedback){
933             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
934         }
935
936         if(this.disabled){
937             return;
938         }
939         
940         if(this.allowBlank && !this.getRawValue().length){
941             return;
942         }
943         
944         if(this.indicator){
945             this.indicator.removeClass('invisible');
946             this.indicator.addClass('visible');
947         }
948         
949         this.el.addClass(this.invalidClass);
950         
951         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
952             
953             var feedback = this.el.select('.form-control-feedback', true).first();
954             
955             if(feedback){
956                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
957                 
958                 if(this.getValue().length || this.forceFeedback){
959                     this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
960                 }
961                 
962             }
963             
964         }
965         
966         this.fireEvent('invalid', this, msg);
967     },
968     // private
969     SafariOnKeyDown : function(event)
970     {
971         // this is a workaround for a password hang bug on chrome/ webkit.
972         if (this.inputEl().dom.type != 'password') {
973             return;
974         }
975         
976         var isSelectAll = false;
977         
978         if(this.inputEl().dom.selectionEnd > 0){
979             isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
980         }
981         if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
982             event.preventDefault();
983             this.setValue('');
984             return;
985         }
986         
987         if(isSelectAll  && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
988             
989             event.preventDefault();
990             // this is very hacky as keydown always get's upper case.
991             //
992             var cc = String.fromCharCode(event.getCharCode());
993             this.setValue( event.shiftKey ?  cc : cc.toLowerCase());
994             
995         }
996     },
997     adjustWidth : function(tag, w){
998         tag = tag.toLowerCase();
999         if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
1000             if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
1001                 if(tag == 'input'){
1002                     return w + 2;
1003                 }
1004                 if(tag == 'textarea'){
1005                     return w-2;
1006                 }
1007             }else if(Roo.isOpera){
1008                 if(tag == 'input'){
1009                     return w + 2;
1010                 }
1011                 if(tag == 'textarea'){
1012                     return w-2;
1013                 }
1014             }
1015         }
1016         return w;
1017     },
1018     
1019     setFieldLabel : function(v)
1020     {
1021         if(!this.rendered){
1022             return;
1023         }
1024         
1025         if(this.indicator){
1026             var ar = this.el.select('label > span',true);
1027             
1028             if (ar.elements.length) {
1029                 this.el.select('label > span',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1030                 this.fieldLabel = v;
1031                 return;
1032             }
1033             
1034             var br = this.el.select('label',true);
1035             
1036             if(br.elements.length) {
1037                 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1038                 this.fieldLabel = v;
1039                 return;
1040             }
1041             
1042             Roo.log('Cannot Found any of label > span || label in input');
1043             return;
1044         }
1045         
1046         this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1047         this.fieldLabel = v;
1048         
1049         
1050     }
1051 });
1052
1053