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