9 * @class Roo.bootstrap.Input
10 * @extends Roo.bootstrap.Component
11 * Bootstrap Input class
12 * @cfg {Boolean} disabled is it disabled
13 * @cfg {String} inputType button | checkbox | email | file | hidden | image | number | password | radio | range | reset | search | submit | text
14 * @cfg {String} name name of the input
15 * @cfg {string} fieldLabel - the label associated
16 * @cfg {string} placeholder - placeholder to put in text.
17 * @cfg {string} before - input group add on before
18 * @cfg {string} after - input group add on after
19 * @cfg {string} size - (lg|sm) or leave empty..
20 * @cfg {Number} xs colspan out of 12 for mobile-sized screens
21 * @cfg {Number} sm colspan out of 12 for tablet-sized screens
22 * @cfg {Number} md colspan out of 12 for computer-sized screens
23 * @cfg {Number} lg colspan out of 12 for large computer-sized screens
24 * @cfg {string} value default value of the input
25 * @cfg {Number} labelWidth set the width of label
26 * @cfg {Number} labellg set the width of label (1-12)
27 * @cfg {Number} labelmd set the width of label (1-12)
28 * @cfg {Number} labelsm set the width of label (1-12)
29 * @cfg {Number} labelxs set the width of label (1-12)
30 * @cfg {String} labelAlign (top|left)
31 * @cfg {Boolean} readOnly Specifies that the field should be read-only
32 * @cfg {String} autocomplete - default is new-password see: https://developers.google.com/web/fundamentals/input/form/label-and-name-inputs?hl=en
33 * @cfg {String} indicatorpos (left|right) default left
34 * @cfg {String} capture (user|camera) use for file input only. (default empty)
35 * @cfg {String} accept (image|video|audio) use for file input only. (default empty)
37 * @cfg {String} align (left|center|right) Default left
38 * @cfg {Boolean} forceFeedback (true|false) Default false
42 * @param {Object} config The config object
45 Roo.bootstrap.Input = function(config){
47 Roo.bootstrap.Input.superclass.constructor.call(this, config);
52 * Fires when this field receives input focus.
53 * @param {Roo.form.Field} this
58 * Fires when this field loses input focus.
59 * @param {Roo.form.Field} this
64 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
65 * {@link Roo.EventObject#getKey} to determine which key was pressed.
66 * @param {Roo.form.Field} this
67 * @param {Roo.EventObject} e The event object
72 * Fires just before the field blurs if the field value has changed.
73 * @param {Roo.form.Field} this
74 * @param {Mixed} newValue The new value
75 * @param {Mixed} oldValue The original value
80 * Fires after the field has been marked as invalid.
81 * @param {Roo.form.Field} this
82 * @param {String} msg The validation message
87 * Fires after the field has been validated with no errors.
88 * @param {Roo.form.Field} this
93 * Fires after the key up
94 * @param {Roo.form.Field} this
95 * @param {Roo.EventObject} e The event Object
101 Roo.extend(Roo.bootstrap.Input, Roo.bootstrap.Component, {
103 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
104 automatic validation (defaults to "keyup").
106 validationEvent : "keyup",
108 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
110 validateOnBlur : true,
112 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
114 validationDelay : 250,
116 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
118 focusClass : "x-form-focus", // not needed???
122 * @cfg {String} invalidClass DEPRICATED - code uses BS4 - is-valid / is-invalid
124 invalidClass : "has-warning",
127 * @cfg {String} validClass DEPRICATED - code uses BS4 - is-valid / is-invalid
129 validClass : "has-success",
132 * @cfg {Boolean} hasFeedback (true|false) default true
137 * @cfg {String} invalidFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
139 invalidFeedbackClass : "glyphicon-warning-sign",
142 * @cfg {String} validFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
144 validFeedbackClass : "glyphicon-ok",
147 * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives input focus (defaults to false)
149 selectOnFocus : false,
152 * @cfg {String} maskRe An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null)
156 * @cfg {String} vtype A validation type name as defined in {@link Roo.form.VTypes} (defaults to null)
161 * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
163 disableKeyFilter : false,
166 * @cfg {Boolean} disabled True to disable the field (defaults to false).
170 * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
174 * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
176 blankText : "Please complete this mandatory field",
179 * @cfg {Number} minLength Minimum input field length required (defaults to 0)
183 * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
185 maxLength : Number.MAX_VALUE,
187 * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to "The minimum length for this field is {minLength}")
189 minLengthText : "The minimum length for this field is {0}",
191 * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to "The maximum length for this field is {maxLength}")
193 maxLengthText : "The maximum length for this field is {0}",
197 * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
198 * If available, this function will be called only after the basic validators all return true, and will be passed the
199 * current field value and expected to return boolean true if the value is valid or a string error message if invalid.
203 * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
204 * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
205 * current field value. If the test fails, the field will be marked invalid using {@link #regexText}.
209 * @cfg {String} regexText -- Depricated - use Invalid Text
214 * @cfg {String} invalidText The error text to display if {@link #validator} test fails during validation (defaults to "")
239 formatedValue : false,
240 forceFeedback : false,
242 indicatorpos : 'left',
252 parentLabelAlign : function()
255 while (parent.parent()) {
256 parent = parent.parent();
257 if (typeof(parent.labelAlign) !='undefined') {
258 return parent.labelAlign;
265 getAutoCreate : function()
267 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
273 if(this.inputType != 'hidden'){
274 cfg.cls = 'form-group' //input-group
280 type : this.inputType,
282 cls : 'form-control',
283 placeholder : this.placeholder || '',
284 autocomplete : this.autocomplete || 'new-password'
287 if(this.capture.length){
288 input.capture = this.capture;
291 if(this.accept.length){
292 input.accept = this.accept + "/*";
296 input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
299 if(this.maxLength && this.maxLength != Number.MAX_VALUE){
300 input.maxLength = this.maxLength;
312 input.name = this.name;
316 input.cls += ' input-' + this.size;
320 ['xs','sm','md','lg'].map(function(size){
321 if (settings[size]) {
322 cfg.cls += ' col-' + size + '-' + settings[size];
326 var inputblock = input;
330 cls: 'glyphicon form-control-feedback'
333 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
336 cls : 'has-feedback',
344 if (this.before || this.after) {
351 if (this.before && typeof(this.before) == 'string') {
355 cls : 'roo-input-before input-group-addon input-group-prepend input-group-text',
359 if (this.before && typeof(this.before) == 'object') {
360 this.before = Roo.factory(this.before);
364 cls : 'roo-input-before input-group-prepend input-group-text input-group-' +
365 (this.before.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
369 inputblock.cn.push(input);
371 if (this.after && typeof(this.after) == 'string') {
374 cls : 'roo-input-after input-group-append input-group-text input-group-addon',
378 if (this.after && typeof(this.after) == 'object') {
379 this.after = Roo.factory(this.after);
383 cls : 'roo-input-after input-group-append input-group-text input-group-' +
384 (this.after.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
388 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
389 inputblock.cls += ' has-feedback';
390 inputblock.cn.push(feedback);
395 cls : 'roo-required-indicator ' + (this.indicatorpos == 'right' ? 'right' : 'left') +'-indicator text-danger fa fa-lg fa-star',
396 tooltip : 'This field is required'
398 if (Roo.bootstrap.version == 4) {
401 style : 'display-none'
404 if (align ==='left' && this.fieldLabel.length) {
406 cfg.cls += ' roo-form-group-label-left' + (Roo.bootstrap.version == 4 ? ' row' : '');
413 cls : 'control-label col-form-label',
414 html : this.fieldLabel
425 var labelCfg = cfg.cn[1];
426 var contentCfg = cfg.cn[2];
428 if(this.indicatorpos == 'right'){
433 cls : 'control-label col-form-label',
437 html : this.fieldLabel
451 labelCfg = cfg.cn[0];
452 contentCfg = cfg.cn[1];
456 if(this.labelWidth > 12){
457 labelCfg.style = "width: " + this.labelWidth + 'px';
460 if(this.labelWidth < 13 && this.labelmd == 0){
461 this.labelmd = this.labelWidth;
464 if(this.labellg > 0){
465 labelCfg.cls += ' col-lg-' + this.labellg;
466 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
469 if(this.labelmd > 0){
470 labelCfg.cls += ' col-md-' + this.labelmd;
471 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
474 if(this.labelsm > 0){
475 labelCfg.cls += ' col-sm-' + this.labelsm;
476 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
479 if(this.labelxs > 0){
480 labelCfg.cls += ' col-xs-' + this.labelxs;
481 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
485 } else if ( this.fieldLabel.length) {
490 cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
491 tooltip : 'This field is required'
495 //cls : 'input-group-addon',
496 html : this.fieldLabel
504 if(this.indicatorpos == 'right'){
509 //cls : 'input-group-addon',
510 html : this.fieldLabel
515 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
516 tooltip : 'This field is required'
536 if (this.parentType === 'Navbar' && this.parent().bar) {
537 cfg.cls += ' navbar-form';
540 if (this.parentType === 'NavGroup' && !(Roo.bootstrap.version == 4 && this.parent().form)) {
541 // on BS4 we do this only if not form
542 cfg.cls += ' navbar-form';
550 * return the real input element.
554 return this.el.select('input.form-control',true).first();
557 tooltipEl : function()
559 return this.inputEl();
562 indicatorEl : function()
564 if (Roo.bootstrap.version == 4) {
565 return false; // not enabled in v4 yet.
568 var indicator = this.el.select('i.roo-required-indicator',true).first();
578 setDisabled : function(v)
580 var i = this.inputEl().dom;
582 i.removeAttribute('disabled');
586 i.setAttribute('disabled','true');
588 initEvents : function()
591 this.inputEl().on("keydown" , this.fireKey, this);
592 this.inputEl().on("focus", this.onFocus, this);
593 this.inputEl().on("blur", this.onBlur, this);
595 this.inputEl().relayEvent('keyup', this);
597 this.indicator = this.indicatorEl();
600 this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible'); // changed from invisible??? -
603 // reference to original value for reset
604 this.originalValue = this.getValue();
605 //Roo.form.TextField.superclass.initEvents.call(this);
606 if(this.validationEvent == 'keyup'){
607 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
608 this.inputEl().on('keyup', this.filterValidation, this);
610 else if(this.validationEvent !== false){
611 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
614 if(this.selectOnFocus){
615 this.on("focus", this.preFocus, this);
618 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
619 this.inputEl().on("keypress", this.filterKeys, this);
621 this.inputEl().relayEvent('keypress', this);
624 this.el.on("keyup", this.onKeyUp, this, {buffer:50});
625 this.el.on("click", this.autoSize, this);
628 if(this.inputEl().is('input[type=password]') && Roo.isSafari){
629 this.inputEl().on('keydown', this.SafariOnKeyDown, this);
632 if (typeof(this.before) == 'object') {
633 this.before.render(this.el.select('.roo-input-before',true).first());
635 if (typeof(this.after) == 'object') {
636 this.after.render(this.el.select('.roo-input-after',true).first());
639 this.inputEl().on('change', this.onChange, this);
642 filterValidation : function(e){
643 if(!e.isNavKeyPress()){
644 this.validationTask.delay(this.validationDelay);
648 * Validates the field value
649 * @return {Boolean} True if the value is valid, else false
651 validate : function(){
652 //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
653 if(this.disabled || this.validateValue(this.getRawValue())){
664 * Validates a value according to the field's validation rules and marks the field as invalid
665 * if the validation fails
666 * @param {Mixed} value The value to validate
667 * @return {Boolean} True if the value is valid, else false
669 validateValue : function(value)
671 if(this.getVisibilityEl().hasClass('hidden')){
675 if(value.length < 1) { // if it's blank
682 if(value.length < this.minLength){
685 if(value.length > this.maxLength){
689 var vt = Roo.form.VTypes;
690 if(!vt[this.vtype](value, this)){
694 if(typeof this.validator == "function"){
695 var msg = this.validator(value);
699 if (typeof(msg) == 'string') {
700 this.invalidText = msg;
704 if(this.regex && !this.regex.test(value)){
712 fireKey : function(e){
713 //Roo.log('field ' + e.getKey());
714 if(e.isNavKeyPress()){
715 this.fireEvent("specialkey", this, e);
718 focus : function (selectText){
720 this.inputEl().focus();
721 if(selectText === true){
722 this.inputEl().dom.select();
728 onFocus : function(){
729 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
730 // this.el.addClass(this.focusClass);
733 this.hasFocus = true;
734 this.startValue = this.getValue();
735 this.fireEvent("focus", this);
739 beforeBlur : Roo.emptyFn,
745 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
746 //this.el.removeClass(this.focusClass);
748 this.hasFocus = false;
749 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
752 var v = this.getValue();
753 if(String(v) !== String(this.startValue)){
754 this.fireEvent('change', this, v, this.startValue);
756 this.fireEvent("blur", this);
759 onChange : function(e)
761 var v = this.getValue();
762 if(String(v) !== String(this.startValue)){
763 this.fireEvent('change', this, v, this.startValue);
769 * Resets the current field value to the originally loaded value and clears any validation messages
772 this.setValue(this.originalValue);
776 * Returns the name of the field
777 * @return {Mixed} name The name field
783 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
784 * @return {Mixed} value The field value
786 getValue : function(){
788 var v = this.inputEl().getValue();
793 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
794 * @return {Mixed} value The field value
796 getRawValue : function(){
797 var v = this.inputEl().getValue();
803 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
804 * @param {Mixed} value The value to set
806 setRawValue : function(v){
807 return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
810 selectText : function(start, end){
811 var v = this.getRawValue();
813 start = start === undefined ? 0 : start;
814 end = end === undefined ? v.length : end;
815 var d = this.inputEl().dom;
816 if(d.setSelectionRange){
817 d.setSelectionRange(start, end);
818 }else if(d.createTextRange){
819 var range = d.createTextRange();
820 range.moveStart("character", start);
821 range.moveEnd("character", v.length-end);
828 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
829 * @param {Mixed} value The value to set
831 setValue : function(v){
834 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
840 processValue : function(value){
841 if(this.stripCharsRe){
842 var newValue = value.replace(this.stripCharsRe, '');
843 if(newValue !== value){
844 this.setRawValue(newValue);
851 preFocus : function(){
853 if(this.selectOnFocus){
854 this.inputEl().dom.select();
857 filterKeys : function(e){
859 if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
862 var c = e.getCharCode(), cc = String.fromCharCode(c);
863 if(Roo.isIE && (e.isSpecialKey() || !cc)){
866 if(!this.maskRe.test(cc)){
871 * Clear any invalid styles/messages for this field
873 clearInvalid : function(){
875 if(!this.el || this.preventMark){ // not rendered
880 this.el.removeClass([this.invalidClass, 'is-invalid']);
882 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
884 var feedback = this.el.select('.form-control-feedback', true).first();
887 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
893 this.indicator.removeClass('visible');
894 this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
897 this.fireEvent('valid', this);
901 * Mark this field as valid
903 markValid : function()
905 if(!this.el || this.preventMark){ // not rendered...
909 this.el.removeClass([this.invalidClass, this.validClass]);
910 this.inputEl().removeClass(['is-valid', 'is-invalid']);
912 var feedback = this.el.select('.form-control-feedback', true).first();
915 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
919 this.indicator.removeClass('visible');
920 this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
927 if(this.allowBlank && !this.getRawValue().length){
930 if (Roo.bootstrap.version == 3) {
931 this.el.addClass(this.validClass);
933 this.inputEl().addClass('is-valid');
936 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
938 var feedback = this.el.select('.form-control-feedback', true).first();
941 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
942 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
947 this.fireEvent('valid', this);
951 * Mark this field as invalid
952 * @param {String} msg The validation message
954 markInvalid : function(msg)
956 if(!this.el || this.preventMark){ // not rendered
960 this.el.removeClass([this.invalidClass, this.validClass]);
961 this.inputEl().removeClass(['is-valid', 'is-invalid']);
963 var feedback = this.el.select('.form-control-feedback', true).first();
966 this.el.select('.form-control-feedback', true).first().removeClass(
967 [this.invalidFeedbackClass, this.validFeedbackClass]);
974 if(this.allowBlank && !this.getRawValue().length){
979 this.indicator.removeClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
980 this.indicator.addClass('visible');
982 if (Roo.bootstrap.version == 3) {
983 this.el.addClass(this.invalidClass);
985 this.inputEl().addClass('is-invalid');
990 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
992 var feedback = this.el.select('.form-control-feedback', true).first();
995 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
997 if(this.getValue().length || this.forceFeedback){
998 this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
1005 this.fireEvent('invalid', this, msg);
1008 SafariOnKeyDown : function(event)
1010 // this is a workaround for a password hang bug on chrome/ webkit.
1011 if (this.inputEl().dom.type != 'password') {
1015 var isSelectAll = false;
1017 if(this.inputEl().dom.selectionEnd > 0){
1018 isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
1020 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
1021 event.preventDefault();
1026 if(isSelectAll && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
1028 event.preventDefault();
1029 // this is very hacky as keydown always get's upper case.
1031 var cc = String.fromCharCode(event.getCharCode());
1032 this.setValue( event.shiftKey ? cc : cc.toLowerCase());
1036 adjustWidth : function(tag, w){
1037 tag = tag.toLowerCase();
1038 if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
1039 if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
1043 if(tag == 'textarea'){
1046 }else if(Roo.isOpera){
1050 if(tag == 'textarea'){
1058 setFieldLabel : function(v)
1064 if(this.indicatorEl()){
1065 var ar = this.el.select('label > span',true);
1067 if (ar.elements.length) {
1068 this.el.select('label > span',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1069 this.fieldLabel = v;
1073 var br = this.el.select('label',true);
1075 if(br.elements.length) {
1076 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1077 this.fieldLabel = v;
1081 Roo.log('Cannot Found any of label > span || label in input');
1085 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1086 this.fieldLabel = v;