9 * @class Roo.bootstrap.Input
10 * @extends Roo.bootstrap.Component
11 * Bootstrap Input class
12 * @cfg {Boolean} disabled is it disabled
13 * @cfg {String} (button|checkbox|email|file|hidden|image|number|password|radio|range|reset|search|submit|text) inputType
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
38 * @cfg {String} align (left|center|right) Default left
39 * @cfg {Boolean} forceFeedback (true|false) Default false
43 * @param {Object} config The config object
46 Roo.bootstrap.Input = function(config){
48 Roo.bootstrap.Input.superclass.constructor.call(this, config);
53 * Fires when this field receives input focus.
54 * @param {Roo.form.Field} this
59 * Fires when this field loses input focus.
60 * @param {Roo.form.Field} this
65 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
66 * {@link Roo.EventObject#getKey} to determine which key was pressed.
67 * @param {Roo.form.Field} this
68 * @param {Roo.EventObject} e The event object
73 * Fires just before the field blurs if the field value has changed.
74 * @param {Roo.form.Field} this
75 * @param {Mixed} newValue The new value
76 * @param {Mixed} oldValue The original value
81 * Fires after the field has been marked as invalid.
82 * @param {Roo.form.Field} this
83 * @param {String} msg The validation message
88 * Fires after the field has been validated with no errors.
89 * @param {Roo.form.Field} this
94 * Fires after the key up
95 * @param {Roo.form.Field} this
96 * @param {Roo.EventObject} e The event Object
102 Roo.extend(Roo.bootstrap.Input, Roo.bootstrap.Component, {
104 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
105 automatic validation (defaults to "keyup").
107 validationEvent : "keyup",
109 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
111 validateOnBlur : true,
113 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
115 validationDelay : 250,
117 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
119 focusClass : "x-form-focus", // not needed???
123 * @cfg {String} invalidClass DEPRICATED - code uses BS4 - is-valid / is-invalid
125 invalidClass : "has-warning",
128 * @cfg {String} validClass DEPRICATED - code uses BS4 - is-valid / is-invalid
130 validClass : "has-success",
133 * @cfg {Boolean} hasFeedback (true|false) default true
138 * @cfg {String} invalidFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
140 invalidFeedbackClass : "glyphicon-warning-sign",
143 * @cfg {String} validFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
145 validFeedbackClass : "glyphicon-ok",
148 * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives input focus (defaults to false)
150 selectOnFocus : false,
153 * @cfg {String} maskRe An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null)
157 * @cfg {String} vtype A validation type name as defined in {@link Roo.form.VTypes} (defaults to null)
162 * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
164 disableKeyFilter : false,
167 * @cfg {Boolean} disabled True to disable the field (defaults to false).
171 * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
175 * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
177 blankText : "Please complete this mandatory field",
180 * @cfg {Number} minLength Minimum input field length required (defaults to 0)
184 * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
186 maxLength : Number.MAX_VALUE,
188 * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to "The minimum length for this field is {minLength}")
190 minLengthText : "The minimum length for this field is {0}",
192 * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to "The maximum length for this field is {maxLength}")
194 maxLengthText : "The maximum length for this field is {0}",
198 * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
199 * If available, this function will be called only after the basic validators all return true, and will be passed the
200 * current field value and expected to return boolean true if the value is valid or a string error message if invalid.
204 * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
205 * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
206 * current field value. If the test fails, the field will be marked invalid using {@link #regexText}.
210 * @cfg {String} regexText -- Depricated - use Invalid Text
215 * @cfg {String} invalidText The error text to display if {@link #validator} test fails during validation (defaults to "")
240 formatedValue : false,
241 forceFeedback : false,
243 indicatorpos : 'left',
253 parentLabelAlign : function()
256 while (parent.parent()) {
257 parent = parent.parent();
258 if (typeof(parent.labelAlign) !='undefined') {
259 return parent.labelAlign;
266 getAutoCreate : function()
268 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
274 if(this.inputType != 'hidden'){
275 cfg.cls = 'form-group' //input-group
281 type : this.inputType,
283 cls : 'form-control',
284 placeholder : this.placeholder || '',
285 autocomplete : this.autocomplete || 'new-password'
287 if (this.inputType == 'file') {
288 input.style = 'overflow:hidden'; // why not in CSS?
291 if(this.capture.length){
292 input.capture = this.capture;
295 if(this.accept.length){
296 input.accept = this.accept + "/*";
300 input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
303 if(this.maxLength && this.maxLength != Number.MAX_VALUE){
304 input.maxLength = this.maxLength;
316 input.name = this.name;
320 input.cls += ' input-' + this.size;
324 ['xs','sm','md','lg'].map(function(size){
325 if (settings[size]) {
326 cfg.cls += ' col-' + size + '-' + settings[size];
330 var inputblock = input;
334 cls: 'glyphicon form-control-feedback'
337 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
340 cls : 'has-feedback',
348 if (this.before || this.after) {
355 if (this.before && typeof(this.before) == 'string') {
359 cls : 'roo-input-before input-group-addon input-group-prepend input-group-text',
363 if (this.before && typeof(this.before) == 'object') {
364 this.before = Roo.factory(this.before);
368 cls : 'roo-input-before input-group-prepend input-group-' +
369 (this.before.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
373 inputblock.cn.push(input);
375 if (this.after && typeof(this.after) == 'string') {
378 cls : 'roo-input-after input-group-append input-group-text input-group-addon',
382 if (this.after && typeof(this.after) == 'object') {
383 this.after = Roo.factory(this.after);
387 cls : 'roo-input-after input-group-append input-group-' +
388 (this.after.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
392 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
393 inputblock.cls += ' has-feedback';
394 inputblock.cn.push(feedback);
399 cls : 'roo-required-indicator ' + (this.indicatorpos == 'right' ? 'right' : 'left') +'-indicator text-danger fa fa-lg fa-star',
400 tooltip : 'This field is required'
402 if (this.allowBlank ) {
403 indicator.style = this.allowBlank ? ' display:none' : '';
405 if (align ==='left' && this.fieldLabel.length) {
407 cfg.cls += ' roo-form-group-label-left' + (Roo.bootstrap.version == 4 ? ' row' : '');
414 cls : 'control-label col-form-label',
415 html : this.fieldLabel
426 var labelCfg = cfg.cn[1];
427 var contentCfg = cfg.cn[2];
429 if(this.indicatorpos == 'right'){
434 cls : 'control-label col-form-label',
438 html : this.fieldLabel
452 labelCfg = cfg.cn[0];
453 contentCfg = cfg.cn[1];
457 if(this.labelWidth > 12){
458 labelCfg.style = "width: " + this.labelWidth + 'px';
461 if(this.labelWidth < 13 && this.labelmd == 0){
462 this.labelmd = this.labelWidth;
465 if(this.labellg > 0){
466 labelCfg.cls += ' col-lg-' + this.labellg;
467 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
470 if(this.labelmd > 0){
471 labelCfg.cls += ' col-md-' + this.labelmd;
472 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
475 if(this.labelsm > 0){
476 labelCfg.cls += ' col-sm-' + this.labelsm;
477 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
480 if(this.labelxs > 0){
481 labelCfg.cls += ' col-xs-' + this.labelxs;
482 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
486 } else if ( this.fieldLabel.length) {
493 cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
494 tooltip : 'This field is required',
495 style : this.allowBlank ? ' display:none' : ''
499 //cls : 'input-group-addon',
500 html : this.fieldLabel
508 if(this.indicatorpos == 'right'){
513 //cls : 'input-group-addon',
514 html : this.fieldLabel
519 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
520 tooltip : 'This field is required',
521 style : this.allowBlank ? ' display:none' : ''
541 if (this.parentType === 'Navbar' && this.parent().bar) {
542 cfg.cls += ' navbar-form';
545 if (this.parentType === 'NavGroup' && !(Roo.bootstrap.version == 4 && this.parent().form)) {
546 // on BS4 we do this only if not form
547 cfg.cls += ' navbar-form';
555 * return the real input element.
559 return this.el.select('input.form-control',true).first();
562 tooltipEl : function()
564 return this.inputEl();
567 indicatorEl : function()
569 if (Roo.bootstrap.version == 4) {
570 return false; // not enabled in v4 yet.
573 var indicator = this.el.select('i.roo-required-indicator',true).first();
583 setDisabled : function(v)
585 var i = this.inputEl().dom;
587 i.removeAttribute('disabled');
591 i.setAttribute('disabled','true');
593 initEvents : function()
596 this.inputEl().on("keydown" , this.fireKey, this);
597 this.inputEl().on("focus", this.onFocus, this);
598 this.inputEl().on("blur", this.onBlur, this);
600 this.inputEl().relayEvent('keyup', this);
602 this.indicator = this.indicatorEl();
605 this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible'); // changed from invisible??? -
608 // reference to original value for reset
609 this.originalValue = this.getValue();
610 //Roo.form.TextField.superclass.initEvents.call(this);
611 if(this.validationEvent == 'keyup'){
612 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
613 this.inputEl().on('keyup', this.filterValidation, this);
615 else if(this.validationEvent !== false){
616 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
619 if(this.selectOnFocus){
620 this.on("focus", this.preFocus, this);
623 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
624 this.inputEl().on("keypress", this.filterKeys, this);
626 this.inputEl().relayEvent('keypress', this);
629 this.el.on("keyup", this.onKeyUp, this, {buffer:50});
630 this.el.on("click", this.autoSize, this);
633 if(this.inputEl().is('input[type=password]') && Roo.isSafari){
634 this.inputEl().on('keydown', this.SafariOnKeyDown, this);
637 if (typeof(this.before) == 'object') {
638 this.before.render(this.el.select('.roo-input-before',true).first());
640 if (typeof(this.after) == 'object') {
641 this.after.render(this.el.select('.roo-input-after',true).first());
644 this.inputEl().on('change', this.onChange, this);
647 filterValidation : function(e){
648 if(!e.isNavKeyPress()){
649 this.validationTask.delay(this.validationDelay);
653 * Validates the field value
654 * @return {Boolean} True if the value is valid, else false
656 validate : function(){
657 //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
658 if(this.disabled || this.validateValue(this.getRawValue())){
669 * Validates a value according to the field's validation rules and marks the field as invalid
670 * if the validation fails
671 * @param {Mixed} value The value to validate
672 * @return {Boolean} True if the value is valid, else false
674 validateValue : function(value)
676 if(this.getVisibilityEl().hasClass('hidden')){
680 if(value.length < 1) { // if it's blank
687 if(value.length < this.minLength){
690 if(value.length > this.maxLength){
694 var vt = Roo.form.VTypes;
695 if(!vt[this.vtype](value, this)){
699 if(typeof this.validator == "function"){
700 var msg = this.validator(value);
704 if (typeof(msg) == 'string') {
705 this.invalidText = msg;
709 if(this.regex && !this.regex.test(value)){
717 fireKey : function(e){
718 //Roo.log('field ' + e.getKey());
719 if(e.isNavKeyPress()){
720 this.fireEvent("specialkey", this, e);
723 focus : function (selectText){
725 this.inputEl().focus();
726 if(selectText === true){
727 this.inputEl().dom.select();
733 onFocus : function(){
734 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
735 // this.el.addClass(this.focusClass);
738 this.hasFocus = true;
739 this.startValue = this.getValue();
740 this.fireEvent("focus", this);
744 beforeBlur : Roo.emptyFn,
750 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
751 //this.el.removeClass(this.focusClass);
753 this.hasFocus = false;
754 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
757 var v = this.getValue();
758 if(String(v) !== String(this.startValue)){
759 this.fireEvent('change', this, v, this.startValue);
761 this.fireEvent("blur", this);
764 onChange : function(e)
766 var v = this.getValue();
767 if(String(v) !== String(this.startValue)){
768 this.fireEvent('change', this, v, this.startValue);
774 * Resets the current field value to the originally loaded value and clears any validation messages
777 this.setValue(this.originalValue);
781 * Returns the name of the field
782 * @return {Mixed} name The name field
788 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
789 * @return {Mixed} value The field value
791 getValue : function(){
793 var v = this.inputEl().getValue();
798 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
799 * @return {Mixed} value The field value
801 getRawValue : function(){
802 var v = this.inputEl().getValue();
808 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
809 * @param {Mixed} value The value to set
811 setRawValue : function(v){
812 return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
815 selectText : function(start, end){
816 var v = this.getRawValue();
818 start = start === undefined ? 0 : start;
819 end = end === undefined ? v.length : end;
820 var d = this.inputEl().dom;
821 if(d.setSelectionRange){
822 d.setSelectionRange(start, end);
823 }else if(d.createTextRange){
824 var range = d.createTextRange();
825 range.moveStart("character", start);
826 range.moveEnd("character", v.length-end);
833 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
834 * @param {Mixed} value The value to set
836 setValue : function(v){
839 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
845 processValue : function(value){
846 if(this.stripCharsRe){
847 var newValue = value.replace(this.stripCharsRe, '');
848 if(newValue !== value){
849 this.setRawValue(newValue);
856 preFocus : function(){
858 if(this.selectOnFocus){
859 this.inputEl().dom.select();
862 filterKeys : function(e){
864 if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
867 var c = e.getCharCode(), cc = String.fromCharCode(c);
868 if(Roo.isIE && (e.isSpecialKey() || !cc)){
871 if(!this.maskRe.test(cc)){
876 * Clear any invalid styles/messages for this field
878 clearInvalid : function(){
880 if(!this.el || this.preventMark){ // not rendered
885 this.el.removeClass([this.invalidClass, 'is-invalid']);
887 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
889 var feedback = this.el.select('.form-control-feedback', true).first();
892 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
898 this.indicator.removeClass('visible');
899 this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
902 this.fireEvent('valid', this);
906 * Mark this field as valid
908 markValid : function()
910 if(!this.el || this.preventMark){ // not rendered...
914 this.el.removeClass([this.invalidClass, this.validClass]);
915 this.inputEl().removeClass(['is-valid', 'is-invalid']);
917 var feedback = this.el.select('.form-control-feedback', true).first();
920 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
924 this.indicator.removeClass('visible');
925 this.indicator.addClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
933 if(this.allowBlank && !this.getRawValue().length){
936 if (Roo.bootstrap.version == 3) {
937 this.el.addClass(this.validClass);
939 this.inputEl().addClass('is-valid');
942 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
944 var feedback = this.el.select('.form-control-feedback', true).first();
947 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
948 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
953 this.fireEvent('valid', this);
957 * Mark this field as invalid
958 * @param {String} msg The validation message
960 markInvalid : function(msg)
962 if(!this.el || this.preventMark){ // not rendered
966 this.el.removeClass([this.invalidClass, this.validClass]);
967 this.inputEl().removeClass(['is-valid', 'is-invalid']);
969 var feedback = this.el.select('.form-control-feedback', true).first();
972 this.el.select('.form-control-feedback', true).first().removeClass(
973 [this.invalidFeedbackClass, this.validFeedbackClass]);
980 if(this.allowBlank && !this.getRawValue().length){
985 this.indicator.removeClass(this.indicatorpos == 'right' ? 'hidden' : 'invisible');
986 this.indicator.addClass('visible');
988 if (Roo.bootstrap.version == 3) {
989 this.el.addClass(this.invalidClass);
991 this.inputEl().addClass('is-invalid');
996 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
998 var feedback = this.el.select('.form-control-feedback', true).first();
1001 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
1003 if(this.getValue().length || this.forceFeedback){
1004 this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
1011 this.fireEvent('invalid', this, msg);
1014 SafariOnKeyDown : function(event)
1016 // this is a workaround for a password hang bug on chrome/ webkit.
1017 if (this.inputEl().dom.type != 'password') {
1021 var isSelectAll = false;
1023 if(this.inputEl().dom.selectionEnd > 0){
1024 isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
1026 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
1027 event.preventDefault();
1032 if(isSelectAll && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
1034 event.preventDefault();
1035 // this is very hacky as keydown always get's upper case.
1037 var cc = String.fromCharCode(event.getCharCode());
1038 this.setValue( event.shiftKey ? cc : cc.toLowerCase());
1042 adjustWidth : function(tag, w){
1043 tag = tag.toLowerCase();
1044 if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
1045 if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
1049 if(tag == 'textarea'){
1052 }else if(Roo.isOpera){
1056 if(tag == 'textarea'){
1064 setFieldLabel : function(v)
1070 if(this.indicatorEl()){
1071 var ar = this.el.select('label > span',true);
1073 if (ar.elements.length) {
1074 this.el.select('label > span',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1075 this.fieldLabel = v;
1079 var br = this.el.select('label',true);
1081 if(br.elements.length) {
1082 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1083 this.fieldLabel = v;
1087 Roo.log('Cannot Found any of label > span || label in input');
1091 this.el.select('label',true).first().dom.innerHTML = (v === null || v === undefined ? '' : v);
1092 this.fieldLabel = v;