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');
583 // reference to original value for reset
584 this.originalValue = this.getValue();
585 //Roo.form.TextField.superclass.initEvents.call(this);
586 if(this.validationEvent == 'keyup'){
587 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
588 this.inputEl().on('keyup', this.filterValidation, this);
590 else if(this.validationEvent !== false){
591 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
594 if(this.selectOnFocus){
595 this.on("focus", this.preFocus, this);
598 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
599 this.inputEl().on("keypress", this.filterKeys, this);
601 this.inputEl().relayEvent('keypress', this);
604 this.el.on("keyup", this.onKeyUp, this, {buffer:50});
605 this.el.on("click", this.autoSize, this);
608 if(this.inputEl().is('input[type=password]') && Roo.isSafari){
609 this.inputEl().on('keydown', this.SafariOnKeyDown, this);
612 if (typeof(this.before) == 'object') {
613 this.before.render(this.el.select('.roo-input-before',true).first());
615 if (typeof(this.after) == 'object') {
616 this.after.render(this.el.select('.roo-input-after',true).first());
621 filterValidation : function(e){
622 if(!e.isNavKeyPress()){
623 this.validationTask.delay(this.validationDelay);
627 * Validates the field value
628 * @return {Boolean} True if the value is valid, else false
630 validate : function(){
631 //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
632 if(this.disabled || this.validateValue(this.getRawValue())){
643 * Validates a value according to the field's validation rules and marks the field as invalid
644 * if the validation fails
645 * @param {Mixed} value The value to validate
646 * @return {Boolean} True if the value is valid, else false
648 validateValue : function(value)
650 if(this.getVisibilityEl().hasClass('hidden')){
654 if(value.length < 1) { // if it's blank
661 if(value.length < this.minLength){
664 if(value.length > this.maxLength){
668 var vt = Roo.form.VTypes;
669 if(!vt[this.vtype](value, this)){
673 if(typeof this.validator == "function"){
674 var msg = this.validator(value);
678 if (typeof(msg) == 'string') {
679 this.invalidText = msg;
683 if(this.regex && !this.regex.test(value)){
691 fireKey : function(e){
692 //Roo.log('field ' + e.getKey());
693 if(e.isNavKeyPress()){
694 this.fireEvent("specialkey", this, e);
697 focus : function (selectText){
699 this.inputEl().focus();
700 if(selectText === true){
701 this.inputEl().dom.select();
707 onFocus : function(){
708 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
709 // this.el.addClass(this.focusClass);
712 this.hasFocus = true;
713 this.startValue = this.getValue();
714 this.fireEvent("focus", this);
718 beforeBlur : Roo.emptyFn,
724 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
725 //this.el.removeClass(this.focusClass);
727 this.hasFocus = false;
728 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
731 var v = this.getValue();
732 if(String(v) !== String(this.startValue)){
733 this.fireEvent('change', this, v, this.startValue);
735 this.fireEvent("blur", this);
739 * Resets the current field value to the originally loaded value and clears any validation messages
742 this.setValue(this.originalValue);
746 * Returns the name of the field
747 * @return {Mixed} name The name field
753 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
754 * @return {Mixed} value The field value
756 getValue : function(){
758 var v = this.inputEl().getValue();
763 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
764 * @return {Mixed} value The field value
766 getRawValue : function(){
767 var v = this.inputEl().getValue();
773 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
774 * @param {Mixed} value The value to set
776 setRawValue : function(v){
777 return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
780 selectText : function(start, end){
781 var v = this.getRawValue();
783 start = start === undefined ? 0 : start;
784 end = end === undefined ? v.length : end;
785 var d = this.inputEl().dom;
786 if(d.setSelectionRange){
787 d.setSelectionRange(start, end);
788 }else if(d.createTextRange){
789 var range = d.createTextRange();
790 range.moveStart("character", start);
791 range.moveEnd("character", v.length-end);
798 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
799 * @param {Mixed} value The value to set
801 setValue : function(v){
804 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
810 processValue : function(value){
811 if(this.stripCharsRe){
812 var newValue = value.replace(this.stripCharsRe, '');
813 if(newValue !== value){
814 this.setRawValue(newValue);
821 preFocus : function(){
823 if(this.selectOnFocus){
824 this.inputEl().dom.select();
827 filterKeys : function(e){
829 if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
832 var c = e.getCharCode(), cc = String.fromCharCode(c);
833 if(Roo.isIE && (e.isSpecialKey() || !cc)){
836 if(!this.maskRe.test(cc)){
841 * Clear any invalid styles/messages for this field
843 clearInvalid : function(){
845 if(!this.el || this.preventMark){ // not rendered
850 this.el.removeClass(this.invalidClass);
852 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
854 var feedback = this.el.select('.form-control-feedback', true).first();
857 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
862 this.fireEvent('valid', this);
866 * Mark this field as valid
868 markValid : function()
870 if(!this.el || this.preventMark){ // not rendered...
874 this.el.removeClass([this.invalidClass, this.validClass]);
876 var feedback = this.el.select('.form-control-feedback', true).first();
879 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
883 this.indicator.removeClass('visible');
884 this.indicator.addClass('invisible');
891 if(this.allowBlank && !this.getRawValue().length){
895 this.el.addClass(this.validClass);
897 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
899 var feedback = this.el.select('.form-control-feedback', true).first();
902 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
903 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
908 this.fireEvent('valid', this);
912 * Mark this field as invalid
913 * @param {String} msg The validation message
915 markInvalid : function(msg)
917 if(!this.el || this.preventMark){ // not rendered
921 this.el.removeClass([this.invalidClass, this.validClass]);
923 var feedback = this.el.select('.form-control-feedback', true).first();
926 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
933 if(this.allowBlank && !this.getRawValue().length){
938 this.indicator.removeClass('invisible');
939 this.indicator.addClass('visible');
942 this.el.addClass(this.invalidClass);
944 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
946 var feedback = this.el.select('.form-control-feedback', true).first();
949 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
951 if(this.getValue().length || this.forceFeedback){
952 this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
959 this.fireEvent('invalid', this, msg);
962 SafariOnKeyDown : function(event)
964 // this is a workaround for a password hang bug on chrome/ webkit.
965 if (this.inputEl().dom.type != 'password') {
969 var isSelectAll = false;
971 if(this.inputEl().dom.selectionEnd > 0){
972 isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
974 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
975 event.preventDefault();
980 if(isSelectAll && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
982 event.preventDefault();
983 // this is very hacky as keydown always get's upper case.
985 var cc = String.fromCharCode(event.getCharCode());
986 this.setValue( event.shiftKey ? cc : cc.toLowerCase());
990 adjustWidth : function(tag, w){
991 tag = tag.toLowerCase();
992 if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
993 if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
997 if(tag == 'textarea'){
1000 }else if(Roo.isOpera){
1004 if(tag == 'textarea'){
1012 setFieldLabel : function(v)
1019 var ar = this.el.select('label > span',true);
1021 if (ar.elements.length) {
1022 this.el.select('label > span',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1023 this.fieldLabel = v;
1027 var br = this.el.select('label',true);
1029 if(br.elements.length) {
1030 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1031 this.fieldLabel = v;
1035 Roo.log('Cannot Found any of label > span || label in input');
1039 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1040 this.fieldLabel = v;