f50b0d67cd8b520d2452ce844dcb3d0b9d4c073d
[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  * @cfg {String} accept (image|video|audio) use for file input only. (default empty)
36  * @cfg {Boolean} preventMark Do not show tick or cross if error/success
37
38  * @cfg {String} align (left|center|right) Default left
39  * @cfg {Boolean} forceFeedback (true|false) Default false
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 DEPRICATED - code uses BS4 - is-valid / is-invalid
124      */
125     invalidClass : "has-warning",
126     
127     /**
128      * @cfg {String} validClass DEPRICATED - code uses BS4 - is-valid / is-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     capture : '',
251     accept : '',
252     
253     parentLabelAlign : function()
254     {
255         var parent = this;
256         while (parent.parent()) {
257             parent = parent.parent();
258             if (typeof(parent.labelAlign) !='undefined') {
259                 return parent.labelAlign;
260             }
261         }
262         return 'left';
263         
264     },
265     
266     getAutoCreate : function()
267     {
268         var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
269         
270         var id = Roo.id();
271         
272         var cfg = {};
273         
274         if(this.inputType != 'hidden'){
275             cfg.cls = 'form-group' //input-group
276         }
277         
278         var input =  {
279             tag: 'input',
280             id : id,
281             type : this.inputType,
282             value : this.value,
283             cls : 'form-control',
284             placeholder : this.placeholder || '',
285             autocomplete : this.autocomplete || 'new-password'
286         };
287         
288         if(this.capture.length){
289             input.capture = this.capture;
290         }
291         
292         if(this.accept.length){
293             input.accept = this.accept + "/*";
294         }
295         
296         if(this.align){
297             input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
298         }
299         
300         if(this.maxLength && this.maxLength != Number.MAX_VALUE){
301             input.maxLength = this.maxLength;
302         }
303         
304         if (this.disabled) {
305             input.disabled=true;
306         }
307         
308         if (this.readOnly) {
309             input.readonly=true;
310         }
311         
312         if (this.name) {
313             input.name = this.name;
314         }
315         
316         if (this.size) {
317             input.cls += ' input-' + this.size;
318         }
319         
320         var settings=this;
321         ['xs','sm','md','lg'].map(function(size){
322             if (settings[size]) {
323                 cfg.cls += ' col-' + size + '-' + settings[size];
324             }
325         });
326         
327         var inputblock = input;
328         
329         var feedback = {
330             tag: 'span',
331             cls: 'glyphicon form-control-feedback'
332         };
333             
334         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
335             
336             inputblock = {
337                 cls : 'has-feedback',
338                 cn :  [
339                     input,
340                     feedback
341                 ] 
342             };  
343         }
344         
345         if (this.before || this.after) {
346             
347             inputblock = {
348                 cls : 'input-group',
349                 cn :  [] 
350             };
351             
352             if (this.before && typeof(this.before) == 'string') {
353                 
354                 inputblock.cn.push({
355                     tag :'span',
356                     cls : 'roo-input-before input-group-addon input-group-prepend input-group-text',
357                     html : this.before
358                 });
359             }
360             if (this.before && typeof(this.before) == 'object') {
361                 this.before = Roo.factory(this.before);
362                 
363                 inputblock.cn.push({
364                     tag :'span',
365                     cls : 'roo-input-before input-group-prepend input-group-text input-group-' +
366                         (this.before.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
367                 });
368             }
369             
370             inputblock.cn.push(input);
371             
372             if (this.after && typeof(this.after) == 'string') {
373                 inputblock.cn.push({
374                     tag :'span',
375                     cls : 'roo-input-after input-group-append input-group-text input-group-addon',
376                     html : this.after
377                 });
378             }
379             if (this.after && typeof(this.after) == 'object') {
380                 this.after = Roo.factory(this.after);
381                 
382                 inputblock.cn.push({
383                     tag :'span',
384                     cls : 'roo-input-after input-group-append input-group-text input-group-' +
385                         (this.after.xtype == 'Button' ? 'btn' : 'addon')  //?? what about checkboxes - that looks like a bit of a hack thought? 
386                 });
387             }
388             
389             if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
390                 inputblock.cls += ' has-feedback';
391                 inputblock.cn.push(feedback);
392             }
393         };
394         var indicator = {
395             tag : 'i',
396             cls : 'roo-required-indicator ' + (this.indicatorpos == 'right'  ? 'right' : 'left') +'-indicator text-danger fa fa-lg fa-star',
397             tooltip : 'This field is required'
398         };
399         if (Roo.bootstrap.version == 4) {
400             indicator = {
401                 tag : 'i',
402                 style : 'display-none'
403             };
404         }
405         if (align ==='left' && this.fieldLabel.length) {
406             
407             cfg.cls += ' roo-form-group-label-left'  + (Roo.bootstrap.version == 4 ? ' row' : '');
408             
409             cfg.cn = [
410                 indicator,
411                 {
412                     tag: 'label',
413                     'for' :  id,
414                     cls : 'control-label col-form-label',
415                     html : this.fieldLabel
416
417                 },
418                 {
419                     cls : "", 
420                     cn: [
421                         inputblock
422                     ]
423                 }
424             ];
425             
426             var labelCfg = cfg.cn[1];
427             var contentCfg = cfg.cn[2];
428             
429             if(this.indicatorpos == 'right'){
430                 cfg.cn = [
431                     {
432                         tag: 'label',
433                         'for' :  id,
434                         cls : 'control-label col-form-label',
435                         cn : [
436                             {
437                                 tag : 'span',
438                                 html : this.fieldLabel
439                             },
440                             indicator
441                         ]
442                     },
443                     {
444                         cls : "",
445                         cn: [
446                             inputblock
447                         ]
448                     }
449
450                 ];
451                 
452                 labelCfg = cfg.cn[0];
453                 contentCfg = cfg.cn[1];
454             
455             }
456             
457             if(this.labelWidth > 12){
458                 labelCfg.style = "width: " + this.labelWidth + 'px';
459             }
460             
461             if(this.labelWidth < 13 && this.labelmd == 0){
462                 this.labelmd = this.labelWidth;
463             }
464             
465             if(this.labellg > 0){
466                 labelCfg.cls += ' col-lg-' + this.labellg;
467                 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
468             }
469             
470             if(this.labelmd > 0){
471                 labelCfg.cls += ' col-md-' + this.labelmd;
472                 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
473             }
474             
475             if(this.labelsm > 0){
476                 labelCfg.cls += ' col-sm-' + this.labelsm;
477                 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
478             }
479             
480             if(this.labelxs > 0){
481                 labelCfg.cls += ' col-xs-' + this.labelxs;
482                 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
483             }
484             
485             
486         } else if ( this.fieldLabel.length) {
487                 
488             cfg.cn = [
489                 {
490                     tag : 'i',
491                     cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
492                     tooltip : 'This field is required'
493                 },
494                 {
495                     tag: 'label',
496                    //cls : 'input-group-addon',
497                     html : this.fieldLabel
498
499                 },
500
501                inputblock
502
503            ];
504            
505            if(this.indicatorpos == 'right'){
506                 
507                 cfg.cn = [
508                     {
509                         tag: 'label',
510                        //cls : 'input-group-addon',
511                         html : this.fieldLabel
512
513                     },
514                     {
515                         tag : 'i',
516                         cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
517                         tooltip : 'This field is required'
518                     },
519
520                    inputblock
521
522                ];
523
524             }
525
526         } else {
527             
528             cfg.cn = [
529
530                     inputblock
531
532             ];
533                 
534                 
535         };
536         
537         if (this.parentType === 'Navbar' &&  this.parent().bar) {
538            cfg.cls += ' navbar-form';
539         }
540         
541         if (this.parentType === 'NavGroup' && !(Roo.bootstrap.version == 4 && this.parent().form)) {
542             // on BS4 we do this only if not form 
543             cfg.cls += ' navbar-form';
544             cfg.tag = 'li';
545         }
546         
547         return cfg;
548         
549     },
550     /**
551      * return the real input element.
552      */
553     inputEl: function ()
554     {
555         return this.el.select('input.form-control',true).first();
556     },
557     
558     tooltipEl : function()
559     {
560         return this.inputEl();
561     },
562     
563     indicatorEl : function()
564     {
565         if (Roo.bootstrap.version == 4) {
566             return false; // not enabled in v4 yet.
567         }
568         
569         var indicator = this.el.select('i.roo-required-indicator',true).first();
570         
571         if(!indicator){
572             return false;
573         }
574         
575         return indicator;
576         
577     },
578     
579     setDisabled : function(v)
580     {
581         var i  = this.inputEl().dom;
582         if (!v) {
583             i.removeAttribute('disabled');
584             return;
585             
586         }
587         i.setAttribute('disabled','true');
588     },
589     initEvents : function()
590     {
591           
592         this.inputEl().on("keydown" , this.fireKey,  this);
593         this.inputEl().on("focus", this.onFocus,  this);
594         this.inputEl().on("blur", this.onBlur,  this);
595         
596         this.inputEl().relayEvent('keyup', this);
597         
598         this.indicator = this.indicatorEl();
599         
600         if(this.indicator){
601             this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible'); // changed from invisible??? - 
602         }
603  
604         // reference to original value for reset
605         this.originalValue = this.getValue();
606         //Roo.form.TextField.superclass.initEvents.call(this);
607         if(this.validationEvent == 'keyup'){
608             this.validationTask = new Roo.util.DelayedTask(this.validate, this);
609             this.inputEl().on('keyup', this.filterValidation, this);
610         }
611         else if(this.validationEvent !== false){
612             this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
613         }
614         
615         if(this.selectOnFocus){
616             this.on("focus", this.preFocus, this);
617             
618         }
619         if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
620             this.inputEl().on("keypress", this.filterKeys, this);
621         } else {
622             this.inputEl().relayEvent('keypress', this);
623         }
624        /* if(this.grow){
625             this.el.on("keyup", this.onKeyUp,  this, {buffer:50});
626             this.el.on("click", this.autoSize,  this);
627         }
628         */
629         if(this.inputEl().is('input[type=password]') && Roo.isSafari){
630             this.inputEl().on('keydown', this.SafariOnKeyDown, this);
631         }
632         
633         if (typeof(this.before) == 'object') {
634             this.before.render(this.el.select('.roo-input-before',true).first());
635         }
636         if (typeof(this.after) == 'object') {
637             this.after.render(this.el.select('.roo-input-after',true).first());
638         }
639         
640         this.inputEl().on('change', this.onChange, this);
641         
642     },
643     filterValidation : function(e){
644         if(!e.isNavKeyPress()){
645             this.validationTask.delay(this.validationDelay);
646         }
647     },
648      /**
649      * Validates the field value
650      * @return {Boolean} True if the value is valid, else false
651      */
652     validate : function(){
653         //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
654         if(this.disabled || this.validateValue(this.getRawValue())){
655             this.markValid();
656             return true;
657         }
658         
659         this.markInvalid();
660         return false;
661     },
662     
663     
664     /**
665      * Validates a value according to the field's validation rules and marks the field as invalid
666      * if the validation fails
667      * @param {Mixed} value The value to validate
668      * @return {Boolean} True if the value is valid, else false
669      */
670     validateValue : function(value)
671     {
672         if(this.getVisibilityEl().hasClass('hidden')){
673             return true;
674         }
675         
676         if(value.length < 1)  { // if it's blank
677             if(this.allowBlank){
678                 return true;
679             }
680             return false;
681         }
682         
683         if(value.length < this.minLength){
684             return false;
685         }
686         if(value.length > this.maxLength){
687             return false;
688         }
689         if(this.vtype){
690             var vt = Roo.form.VTypes;
691             if(!vt[this.vtype](value, this)){
692                 return false;
693             }
694         }
695         if(typeof this.validator == "function"){
696             var msg = this.validator(value);
697             if(msg !== true){
698                 return false;
699             }
700             if (typeof(msg) == 'string') {
701                 this.invalidText = msg;
702             }
703         }
704         
705         if(this.regex && !this.regex.test(value)){
706             return false;
707         }
708         
709         return true;
710     },
711     
712      // private
713     fireKey : function(e){
714         //Roo.log('field ' + e.getKey());
715         if(e.isNavKeyPress()){
716             this.fireEvent("specialkey", this, e);
717         }
718     },
719     focus : function (selectText){
720         if(this.rendered){
721             this.inputEl().focus();
722             if(selectText === true){
723                 this.inputEl().dom.select();
724             }
725         }
726         return this;
727     } ,
728     
729     onFocus : function(){
730         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
731            // this.el.addClass(this.focusClass);
732         }
733         if(!this.hasFocus){
734             this.hasFocus = true;
735             this.startValue = this.getValue();
736             this.fireEvent("focus", this);
737         }
738     },
739     
740     beforeBlur : Roo.emptyFn,
741
742     
743     // private
744     onBlur : function(){
745         this.beforeBlur();
746         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
747             //this.el.removeClass(this.focusClass);
748         }
749         this.hasFocus = false;
750         if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
751             this.validate();
752         }
753         var v = this.getValue();
754         if(String(v) !== String(this.startValue)){
755             this.fireEvent('change', this, v, this.startValue);
756         }
757         this.fireEvent("blur", this);
758     },
759     
760     onChange : function(e)
761     {
762         var v = this.getValue();
763         if(String(v) !== String(this.startValue)){
764             this.fireEvent('change', this, v, this.startValue);
765         }
766         
767     },
768     
769     /**
770      * Resets the current field value to the originally loaded value and clears any validation messages
771      */
772     reset : function(){
773         this.setValue(this.originalValue);
774         this.validate();
775     },
776      /**
777      * Returns the name of the field
778      * @return {Mixed} name The name field
779      */
780     getName: function(){
781         return this.name;
782     },
783      /**
784      * Returns the normalized data value (undefined or emptyText will be returned as '').  To return the raw value see {@link #getRawValue}.
785      * @return {Mixed} value The field value
786      */
787     getValue : function(){
788         
789         var v = this.inputEl().getValue();
790         
791         return v;
792     },
793     /**
794      * Returns the raw data value which may or may not be a valid, defined value.  To return a normalized value see {@link #getValue}.
795      * @return {Mixed} value The field value
796      */
797     getRawValue : function(){
798         var v = this.inputEl().getValue();
799         
800         return v;
801     },
802     
803     /**
804      * Sets the underlying DOM field's value directly, bypassing validation.  To set the value with validation see {@link #setValue}.
805      * @param {Mixed} value The value to set
806      */
807     setRawValue : function(v){
808         return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
809     },
810     
811     selectText : function(start, end){
812         var v = this.getRawValue();
813         if(v.length > 0){
814             start = start === undefined ? 0 : start;
815             end = end === undefined ? v.length : end;
816             var d = this.inputEl().dom;
817             if(d.setSelectionRange){
818                 d.setSelectionRange(start, end);
819             }else if(d.createTextRange){
820                 var range = d.createTextRange();
821                 range.moveStart("character", start);
822                 range.moveEnd("character", v.length-end);
823                 range.select();
824             }
825         }
826     },
827     
828     /**
829      * Sets a data value into the field and validates it.  To set the value directly without validation see {@link #setRawValue}.
830      * @param {Mixed} value The value to set
831      */
832     setValue : function(v){
833         this.value = v;
834         if(this.rendered){
835             this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
836             this.validate();
837         }
838     },
839     
840     /*
841     processValue : function(value){
842         if(this.stripCharsRe){
843             var newValue = value.replace(this.stripCharsRe, '');
844             if(newValue !== value){
845                 this.setRawValue(newValue);
846                 return newValue;
847             }
848         }
849         return value;
850     },
851   */
852     preFocus : function(){
853         
854         if(this.selectOnFocus){
855             this.inputEl().dom.select();
856         }
857     },
858     filterKeys : function(e){
859         var k = e.getKey();
860         if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
861             return;
862         }
863         var c = e.getCharCode(), cc = String.fromCharCode(c);
864         if(Roo.isIE && (e.isSpecialKey() || !cc)){
865             return;
866         }
867         if(!this.maskRe.test(cc)){
868             e.stopEvent();
869         }
870     },
871      /**
872      * Clear any invalid styles/messages for this field
873      */
874     clearInvalid : function(){
875         
876         if(!this.el || this.preventMark){ // not rendered
877             return;
878         }
879         
880         
881         this.el.removeClass([this.invalidClass, 'is-invalid']);
882         
883         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
884             
885             var feedback = this.el.select('.form-control-feedback', true).first();
886             
887             if(feedback){
888                 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
889             }
890             
891         }
892         
893         if(this.indicator){
894             this.indicator.removeClass('visible');
895             this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
896         }
897         
898         this.fireEvent('valid', this);
899     },
900     
901      /**
902      * Mark this field as valid
903      */
904     markValid : function()
905     {
906         if(!this.el  || this.preventMark){ // not rendered...
907             return;
908         }
909         
910         this.el.removeClass([this.invalidClass, this.validClass]);
911         this.inputEl().removeClass(['is-valid', 'is-invalid']);
912
913         var feedback = this.el.select('.form-control-feedback', true).first();
914             
915         if(feedback){
916             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
917         }
918         
919         if(this.indicator){
920             this.indicator.removeClass('visible');
921             this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
922         }
923         
924         if(this.disabled){
925             return;
926         }
927         
928            
929         if(this.allowBlank && !this.getRawValue().length){
930             return;
931         }
932         if (Roo.bootstrap.version == 3) {
933             this.el.addClass(this.validClass);
934         } else {
935             this.inputEl().addClass('is-valid');
936         }
937
938         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
939             
940             var feedback = this.el.select('.form-control-feedback', true).first();
941             
942             if(feedback){
943                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
944                 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
945             }
946             
947         }
948         
949         this.fireEvent('valid', this);
950     },
951     
952      /**
953      * Mark this field as invalid
954      * @param {String} msg The validation message
955      */
956     markInvalid : function(msg)
957     {
958         if(!this.el  || this.preventMark){ // not rendered
959             return;
960         }
961         
962         this.el.removeClass([this.invalidClass, this.validClass]);
963         this.inputEl().removeClass(['is-valid', 'is-invalid']);
964         
965         var feedback = this.el.select('.form-control-feedback', true).first();
966             
967         if(feedback){
968             this.el.select('.form-control-feedback', true).first().removeClass(
969                     [this.invalidFeedbackClass, this.validFeedbackClass]);
970         }
971
972         if(this.disabled){
973             return;
974         }
975         
976         if(this.allowBlank && !this.getRawValue().length){
977             return;
978         }
979         
980         if(this.indicator){
981             this.indicator.removeClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
982             this.indicator.addClass('visible');
983         }
984         if (Roo.bootstrap.version == 3) {
985             this.el.addClass(this.invalidClass);
986         } else {
987             this.inputEl().addClass('is-invalid');
988         }
989         
990         
991         
992         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
993             
994             var feedback = this.el.select('.form-control-feedback', true).first();
995             
996             if(feedback){
997                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
998                 
999                 if(this.getValue().length || this.forceFeedback){
1000                     this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
1001                 }
1002                 
1003             }
1004             
1005         }
1006         
1007         this.fireEvent('invalid', this, msg);
1008     },
1009     // private
1010     SafariOnKeyDown : function(event)
1011     {
1012         // this is a workaround for a password hang bug on chrome/ webkit.
1013         if (this.inputEl().dom.type != 'password') {
1014             return;
1015         }
1016         
1017         var isSelectAll = false;
1018         
1019         if(this.inputEl().dom.selectionEnd > 0){
1020             isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
1021         }
1022         if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
1023             event.preventDefault();
1024             this.setValue('');
1025             return;
1026         }
1027         
1028         if(isSelectAll  && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
1029             
1030             event.preventDefault();
1031             // this is very hacky as keydown always get's upper case.
1032             //
1033             var cc = String.fromCharCode(event.getCharCode());
1034             this.setValue( event.shiftKey ?  cc : cc.toLowerCase());
1035             
1036         }
1037     },
1038     adjustWidth : function(tag, w){
1039         tag = tag.toLowerCase();
1040         if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
1041             if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
1042                 if(tag == 'input'){
1043                     return w + 2;
1044                 }
1045                 if(tag == 'textarea'){
1046                     return w-2;
1047                 }
1048             }else if(Roo.isOpera){
1049                 if(tag == 'input'){
1050                     return w + 2;
1051                 }
1052                 if(tag == 'textarea'){
1053                     return w-2;
1054                 }
1055             }
1056         }
1057         return w;
1058     },
1059     
1060     setFieldLabel : function(v)
1061     {
1062         if(!this.rendered){
1063             return;
1064         }
1065         
1066         if(this.indicatorEl()){
1067             var ar = this.el.select('label > span',true);
1068             
1069             if (ar.elements.length) {
1070                 this.el.select('label > span',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1071                 this.fieldLabel = v;
1072                 return;
1073             }
1074             
1075             var br = this.el.select('label',true);
1076             
1077             if(br.elements.length) {
1078                 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1079                 this.fieldLabel = v;
1080                 return;
1081             }
1082             
1083             Roo.log('Cannot Found any of label > span || label in input');
1084             return;
1085         }
1086         
1087         this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1088         this.fieldLabel = v;
1089         
1090         
1091     }
1092 });
1093
1094