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
35 * @cfg {String} align (left|center|right) Default left
36 * @cfg {Boolean} forceFeedback (true|false) Default false
40 * @param {Object} config The config object
43 Roo.bootstrap.Input = function(config){
45 Roo.bootstrap.Input.superclass.constructor.call(this, config);
50 * Fires when this field receives input focus.
51 * @param {Roo.form.Field} this
56 * Fires when this field loses input focus.
57 * @param {Roo.form.Field} this
62 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
63 * {@link Roo.EventObject#getKey} to determine which key was pressed.
64 * @param {Roo.form.Field} this
65 * @param {Roo.EventObject} e The event object
70 * Fires just before the field blurs if the field value has changed.
71 * @param {Roo.form.Field} this
72 * @param {Mixed} newValue The new value
73 * @param {Mixed} oldValue The original value
78 * Fires after the field has been marked as invalid.
79 * @param {Roo.form.Field} this
80 * @param {String} msg The validation message
85 * Fires after the field has been validated with no errors.
86 * @param {Roo.form.Field} this
91 * Fires after the key up
92 * @param {Roo.form.Field} this
93 * @param {Roo.EventObject} e The event Object
99 Roo.extend(Roo.bootstrap.Input, Roo.bootstrap.Component, {
101 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
102 automatic validation (defaults to "keyup").
104 validationEvent : "keyup",
106 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
108 validateOnBlur : true,
110 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
112 validationDelay : 250,
114 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
116 focusClass : "x-form-focus", // not needed???
120 * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
122 invalidClass : "has-warning",
125 * @cfg {String} validClass The CSS class to use when marking a field valid (defaults to "x-form-invalid")
127 validClass : "has-success",
130 * @cfg {Boolean} hasFeedback (true|false) default true
135 * @cfg {String} invalidFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
137 invalidFeedbackClass : "glyphicon-warning-sign",
140 * @cfg {String} validFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
142 validFeedbackClass : "glyphicon-ok",
145 * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives input focus (defaults to false)
147 selectOnFocus : false,
150 * @cfg {String} maskRe An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null)
154 * @cfg {String} vtype A validation type name as defined in {@link Roo.form.VTypes} (defaults to null)
159 * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
161 disableKeyFilter : false,
164 * @cfg {Boolean} disabled True to disable the field (defaults to false).
168 * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
172 * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
174 blankText : "Please complete this mandatory field",
177 * @cfg {Number} minLength Minimum input field length required (defaults to 0)
181 * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
183 maxLength : Number.MAX_VALUE,
185 * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to "The minimum length for this field is {minLength}")
187 minLengthText : "The minimum length for this field is {0}",
189 * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to "The maximum length for this field is {maxLength}")
191 maxLengthText : "The maximum length for this field is {0}",
195 * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
196 * If available, this function will be called only after the basic validators all return true, and will be passed the
197 * current field value and expected to return boolean true if the value is valid or a string error message if invalid.
201 * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
202 * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
203 * current field value. If the test fails, the field will be marked invalid using {@link #regexText}.
207 * @cfg {String} regexText -- Depricated - use Invalid Text
212 * @cfg {String} invalidText The error text to display if {@link #validator} test fails during validation (defaults to "")
237 formatedValue : false,
238 forceFeedback : false,
240 indicatorpos : 'left',
247 parentLabelAlign : function()
250 while (parent.parent()) {
251 parent = parent.parent();
252 if (typeof(parent.labelAlign) !='undefined') {
253 return parent.labelAlign;
260 getAutoCreate : function()
262 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
268 if(this.inputType != 'hidden'){
269 cfg.cls = 'form-group' //input-group
275 type : this.inputType,
277 cls : 'form-control',
278 placeholder : this.placeholder || '',
279 autocomplete : this.autocomplete || 'new-password'
283 input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
286 if(this.maxLength && this.maxLength != Number.MAX_VALUE){
287 input.maxLength = this.maxLength;
299 input.name = this.name;
303 input.cls += ' input-' + this.size;
307 ['xs','sm','md','lg'].map(function(size){
308 if (settings[size]) {
309 cfg.cls += ' col-' + size + '-' + settings[size];
313 var inputblock = input;
317 cls: 'glyphicon form-control-feedback'
320 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
323 cls : 'has-feedback',
331 if (this.before || this.after) {
338 if (this.before && typeof(this.before) == 'string') {
342 cls : 'roo-input-before input-group-addon',
346 if (this.before && typeof(this.before) == 'object') {
347 this.before = Roo.factory(this.before);
351 cls : 'roo-input-before input-group-' +
352 (this.before.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
356 inputblock.cn.push(input);
358 if (this.after && typeof(this.after) == 'string') {
361 cls : 'roo-input-after input-group-addon',
365 if (this.after && typeof(this.after) == 'object') {
366 this.after = Roo.factory(this.after);
370 cls : 'roo-input-after input-group-' +
371 (this.after.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
375 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
376 inputblock.cls += ' has-feedback';
377 inputblock.cn.push(feedback);
381 if (align ==='left' && this.fieldLabel.length) {
383 cfg.cls += ' roo-form-group-label-left';
388 cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
389 tooltip : 'This field is required'
394 cls : 'control-label',
395 html : this.fieldLabel
406 var labelCfg = cfg.cn[1];
407 var contentCfg = cfg.cn[2];
409 if(this.indicatorpos == 'right'){
414 cls : 'control-label',
418 html : this.fieldLabel
422 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
423 tooltip : 'This field is required'
436 labelCfg = cfg.cn[0];
437 contentCfg = cfg.cn[1];
441 if(this.labelWidth > 12){
442 labelCfg.style = "width: " + this.labelWidth + 'px';
445 if(this.labelWidth < 13 && this.labelmd == 0){
446 this.labelmd = this.labelWidth;
449 if(this.labellg > 0){
450 labelCfg.cls += ' col-lg-' + this.labellg;
451 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
454 if(this.labelmd > 0){
455 labelCfg.cls += ' col-md-' + this.labelmd;
456 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
459 if(this.labelsm > 0){
460 labelCfg.cls += ' col-sm-' + this.labelsm;
461 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
464 if(this.labelxs > 0){
465 labelCfg.cls += ' col-xs-' + this.labelxs;
466 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
470 } else if ( this.fieldLabel.length) {
475 cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
476 tooltip : 'This field is required'
480 //cls : 'input-group-addon',
481 html : this.fieldLabel
489 if(this.indicatorpos == 'right'){
494 //cls : 'input-group-addon',
495 html : this.fieldLabel
500 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
501 tooltip : 'This field is required'
521 if (this.parentType === 'Navbar' && this.parent().bar) {
522 cfg.cls += ' navbar-form';
525 if (this.parentType === 'NavGroup') {
526 cfg.cls += ' navbar-form';
534 * return the real input element.
538 return this.el.select('input.form-control',true).first();
541 tooltipEl : function()
543 return this.inputEl();
546 indicatorEl : function()
548 var indicator = this.el.select('i.roo-required-indicator',true).first();
558 setDisabled : function(v)
560 var i = this.inputEl().dom;
562 i.removeAttribute('disabled');
566 i.setAttribute('disabled','true');
568 initEvents : function()
571 this.inputEl().on("keydown" , this.fireKey, this);
572 this.inputEl().on("focus", this.onFocus, this);
573 this.inputEl().on("blur", this.onBlur, this);
575 this.inputEl().relayEvent('keyup', this);
577 this.indicator = this.indicatorEl();
580 this.indicator.addClass('invisible');
584 // reference to original value for reset
585 this.originalValue = this.getValue();
586 //Roo.form.TextField.superclass.initEvents.call(this);
587 if(this.validationEvent == 'keyup'){
588 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
589 this.inputEl().on('keyup', this.filterValidation, this);
591 else if(this.validationEvent !== false){
592 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
595 if(this.selectOnFocus){
596 this.on("focus", this.preFocus, this);
599 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
600 this.inputEl().on("keypress", this.filterKeys, this);
602 this.inputEl().relayEvent('keypress', this);
605 this.el.on("keyup", this.onKeyUp, this, {buffer:50});
606 this.el.on("click", this.autoSize, this);
609 if(this.inputEl().is('input[type=password]') && Roo.isSafari){
610 this.inputEl().on('keydown', this.SafariOnKeyDown, this);
613 if (typeof(this.before) == 'object') {
614 this.before.render(this.el.select('.roo-input-before',true).first());
616 if (typeof(this.after) == 'object') {
617 this.after.render(this.el.select('.roo-input-after',true).first());
622 filterValidation : function(e){
623 if(!e.isNavKeyPress()){
624 this.validationTask.delay(this.validationDelay);
628 * Validates the field value
629 * @return {Boolean} True if the value is valid, else false
631 validate : function(){
632 //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
633 if(this.disabled || this.validateValue(this.getRawValue())){
644 * Validates a value according to the field's validation rules and marks the field as invalid
645 * if the validation fails
646 * @param {Mixed} value The value to validate
647 * @return {Boolean} True if the value is valid, else false
649 validateValue : function(value)
651 if(this.getVisibilityEl().hasClass('hidden') || !this.getVisibilityEl().isVisible()){
655 if(value.length < 1) { // if it's blank
662 if(value.length < this.minLength){
665 if(value.length > this.maxLength){
669 var vt = Roo.form.VTypes;
670 if(!vt[this.vtype](value, this)){
674 if(typeof this.validator == "function"){
675 var msg = this.validator(value);
679 if (typeof(msg) == 'string') {
680 this.invalidText = msg;
684 if(this.regex && !this.regex.test(value)){
692 fireKey : function(e){
693 //Roo.log('field ' + e.getKey());
694 if(e.isNavKeyPress()){
695 this.fireEvent("specialkey", this, e);
698 focus : function (selectText){
700 this.inputEl().focus();
701 if(selectText === true){
702 this.inputEl().dom.select();
708 onFocus : function(){
709 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
710 // this.el.addClass(this.focusClass);
713 this.hasFocus = true;
714 this.startValue = this.getValue();
715 this.fireEvent("focus", this);
719 beforeBlur : Roo.emptyFn,
725 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
726 //this.el.removeClass(this.focusClass);
728 this.hasFocus = false;
729 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
732 var v = this.getValue();
733 if(String(v) !== String(this.startValue)){
734 this.fireEvent('change', this, v, this.startValue);
736 this.fireEvent("blur", this);
740 * Resets the current field value to the originally loaded value and clears any validation messages
743 this.setValue(this.originalValue);
747 * Returns the name of the field
748 * @return {Mixed} name The name field
754 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
755 * @return {Mixed} value The field value
757 getValue : function(){
759 var v = this.inputEl().getValue();
764 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
765 * @return {Mixed} value The field value
767 getRawValue : function(){
768 var v = this.inputEl().getValue();
774 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
775 * @param {Mixed} value The value to set
777 setRawValue : function(v){
778 return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
781 selectText : function(start, end){
782 var v = this.getRawValue();
784 start = start === undefined ? 0 : start;
785 end = end === undefined ? v.length : end;
786 var d = this.inputEl().dom;
787 if(d.setSelectionRange){
788 d.setSelectionRange(start, end);
789 }else if(d.createTextRange){
790 var range = d.createTextRange();
791 range.moveStart("character", start);
792 range.moveEnd("character", v.length-end);
799 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
800 * @param {Mixed} value The value to set
802 setValue : function(v){
805 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
811 processValue : function(value){
812 if(this.stripCharsRe){
813 var newValue = value.replace(this.stripCharsRe, '');
814 if(newValue !== value){
815 this.setRawValue(newValue);
822 preFocus : function(){
824 if(this.selectOnFocus){
825 this.inputEl().dom.select();
828 filterKeys : function(e){
830 if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
833 var c = e.getCharCode(), cc = String.fromCharCode(c);
834 if(Roo.isIE && (e.isSpecialKey() || !cc)){
837 if(!this.maskRe.test(cc)){
842 * Clear any invalid styles/messages for this field
844 clearInvalid : function(){
846 if(!this.el || this.preventMark){ // not rendered
851 this.el.removeClass(this.invalidClass);
853 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
855 var feedback = this.el.select('.form-control-feedback', true).first();
858 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
863 this.fireEvent('valid', this);
867 * Mark this field as valid
869 markValid : function()
871 if(!this.el || this.preventMark){ // not rendered...
875 this.el.removeClass([this.invalidClass, this.validClass]);
877 var feedback = this.el.select('.form-control-feedback', true).first();
880 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
884 this.indicator.removeClass('visible');
885 this.indicator.addClass('invisible');
892 if(this.allowBlank && !this.getRawValue().length){
896 this.el.addClass(this.validClass);
898 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
900 var feedback = this.el.select('.form-control-feedback', true).first();
903 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
904 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
909 this.fireEvent('valid', this);
913 * Mark this field as invalid
914 * @param {String} msg The validation message
916 markInvalid : function(msg)
918 if(!this.el || this.preventMark){ // not rendered
922 this.el.removeClass([this.invalidClass, this.validClass]);
924 var feedback = this.el.select('.form-control-feedback', true).first();
927 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
934 if(this.allowBlank && !this.getRawValue().length){
939 this.indicator.removeClass('invisible');
940 this.indicator.addClass('visible');
943 this.el.addClass(this.invalidClass);
945 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
947 var feedback = this.el.select('.form-control-feedback', true).first();
950 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
952 if(this.getValue().length || this.forceFeedback){
953 this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
960 this.fireEvent('invalid', this, msg);
963 SafariOnKeyDown : function(event)
965 // this is a workaround for a password hang bug on chrome/ webkit.
966 if (this.inputEl().dom.type != 'password') {
970 var isSelectAll = false;
972 if(this.inputEl().dom.selectionEnd > 0){
973 isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
975 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
976 event.preventDefault();
981 if(isSelectAll && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
983 event.preventDefault();
984 // this is very hacky as keydown always get's upper case.
986 var cc = String.fromCharCode(event.getCharCode());
987 this.setValue( event.shiftKey ? cc : cc.toLowerCase());
991 adjustWidth : function(tag, w){
992 tag = tag.toLowerCase();
993 if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
994 if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
998 if(tag == 'textarea'){
1001 }else if(Roo.isOpera){
1005 if(tag == 'textarea'){
1013 setFieldLabel : function(v)
1020 var ar = this.el.select('label > span',true);
1022 if (ar.elements.length) {
1023 this.el.select('label > span',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1024 this.fieldLabel = v;
1028 var br = this.el.select('label',true);
1030 if(br.elements.length) {
1031 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1032 this.fieldLabel = v;
1036 Roo.log('Cannot Found any of label > span || label in input');
1040 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1041 this.fieldLabel = v;