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