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
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 The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
125 invalidClass : "has-warning",
128 * @cfg {String} validClass The CSS class to use when marking a field valid (defaults to "x-form-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 The error text to display if {@link #regex} is used and the test fails during validation (defaults to "")
233 formatedValue : false,
234 forceFeedback : false,
236 indicatorpos : 'left',
243 parentLabelAlign : function()
246 while (parent.parent()) {
247 parent = parent.parent();
248 if (typeof(parent.labelAlign) !='undefined') {
249 return parent.labelAlign;
256 getAutoCreate : function()
258 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
264 if(this.inputType != 'hidden'){
265 cfg.cls = 'form-group' //input-group
271 type : this.inputType,
273 cls : 'form-control',
274 placeholder : this.placeholder || '',
275 autocomplete : this.autocomplete || 'new-password'
279 input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
282 if(this.maxLength && this.maxLength != Number.MAX_VALUE){
283 input.maxLength = this.maxLength;
295 input.name = this.name;
299 input.cls += ' input-' + this.size;
303 ['xs','sm','md','lg'].map(function(size){
304 if (settings[size]) {
305 cfg.cls += ' col-' + size + '-' + settings[size];
309 var inputblock = input;
313 cls: 'glyphicon form-control-feedback'
316 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
319 cls : 'has-feedback',
327 if (this.before || this.after) {
334 if (this.before && typeof(this.before) == 'string') {
338 cls : 'roo-input-before input-group-addon',
342 if (this.before && typeof(this.before) == 'object') {
343 this.before = Roo.factory(this.before);
347 cls : 'roo-input-before input-group-' +
348 (this.before.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
352 inputblock.cn.push(input);
354 if (this.after && typeof(this.after) == 'string') {
357 cls : 'roo-input-after input-group-addon',
361 if (this.after && typeof(this.after) == 'object') {
362 this.after = Roo.factory(this.after);
366 cls : 'roo-input-after input-group-' +
367 (this.after.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
371 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
372 inputblock.cls += ' has-feedback';
373 inputblock.cn.push(feedback);
377 if (align ==='left' && this.fieldLabel.length) {
379 cfg.cls += ' roo-form-group-label-left';
384 cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
385 tooltip : 'This field is required'
390 cls : 'control-label',
391 html : this.fieldLabel
402 var labelCfg = cfg.cn[1];
403 var contentCfg = cfg.cn[2];
405 if(this.indicatorpos == 'right'){
410 cls : 'control-label',
411 html : this.fieldLabel
416 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
417 tooltip : 'This field is required'
428 labelCfg = cfg.cn[0];
429 contentCfg = cfg.cn[2];
433 if(this.labelWidth > 12){
434 labelCfg.style = "width: " + this.labelWidth + 'px';
437 if(this.labelWidth < 13 && this.labelmd == 0){
438 this.labelmd = this.labelWidth;
441 if(this.labellg > 0){
442 labelCfg.cls += ' col-lg-' + this.labellg;
443 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
446 if(this.labelmd > 0){
447 labelCfg.cls += ' col-md-' + this.labelmd;
448 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
451 if(this.labelsm > 0){
452 labelCfg.cls += ' col-sm-' + this.labelsm;
453 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
456 if(this.labelxs > 0){
457 labelCfg.cls += ' col-xs-' + this.labelxs;
458 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
462 } else if ( this.fieldLabel.length) {
467 cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
468 tooltip : 'This field is required'
472 //cls : 'input-group-addon',
473 html : this.fieldLabel
481 if(this.indicatorpos == 'right'){
486 //cls : 'input-group-addon',
487 html : this.fieldLabel
492 cls : 'roo-required-indicator right-indicator text-danger fa fa-lg fa-star',
493 tooltip : 'This field is required'
513 if (this.parentType === 'Navbar' && this.parent().bar) {
514 cfg.cls += ' navbar-form';
517 if (this.parentType === 'NavGroup') {
518 cfg.cls += ' navbar-form';
526 * return the real input element.
530 return this.el.select('input.form-control',true).first();
533 tooltipEl : function()
535 return this.inputEl();
538 indicatorEl : function()
540 var indicator = this.el.select('i.roo-required-indicator',true).first();
550 setDisabled : function(v)
552 var i = this.inputEl().dom;
554 i.removeAttribute('disabled');
558 i.setAttribute('disabled','true');
560 initEvents : function()
563 this.inputEl().on("keydown" , this.fireKey, this);
564 this.inputEl().on("focus", this.onFocus, this);
565 this.inputEl().on("blur", this.onBlur, this);
567 this.inputEl().relayEvent('keyup', this);
569 this.indicator = this.indicatorEl();
572 this.indicator.setVisibilityMode(Roo.Element.DISPLAY);
573 this.indicator.hide();
576 // reference to original value for reset
577 this.originalValue = this.getValue();
578 //Roo.form.TextField.superclass.initEvents.call(this);
579 if(this.validationEvent == 'keyup'){
580 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
581 this.inputEl().on('keyup', this.filterValidation, this);
583 else if(this.validationEvent !== false){
584 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
587 if(this.selectOnFocus){
588 this.on("focus", this.preFocus, this);
591 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
592 this.inputEl().on("keypress", this.filterKeys, this);
594 this.inputEl().relayEvent('keypress', this);
597 this.el.on("keyup", this.onKeyUp, this, {buffer:50});
598 this.el.on("click", this.autoSize, this);
601 if(this.inputEl().is('input[type=password]') && Roo.isSafari){
602 this.inputEl().on('keydown', this.SafariOnKeyDown, this);
605 if (typeof(this.before) == 'object') {
606 this.before.render(this.el.select('.roo-input-before',true).first());
608 if (typeof(this.after) == 'object') {
609 this.after.render(this.el.select('.roo-input-after',true).first());
614 filterValidation : function(e){
615 if(!e.isNavKeyPress()){
616 this.validationTask.delay(this.validationDelay);
620 * Validates the field value
621 * @return {Boolean} True if the value is valid, else false
623 validate : function(){
624 //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
625 if(this.disabled || this.validateValue(this.getRawValue())){
636 * Validates a value according to the field's validation rules and marks the field as invalid
637 * if the validation fails
638 * @param {Mixed} value The value to validate
639 * @return {Boolean} True if the value is valid, else false
641 validateValue : function(value){
642 if(value.length < 1) { // if it's blank
649 if(value.length < this.minLength){
652 if(value.length > this.maxLength){
656 var vt = Roo.form.VTypes;
657 if(!vt[this.vtype](value, this)){
661 if(typeof this.validator == "function"){
662 var msg = this.validator(value);
668 if(this.regex && !this.regex.test(value)){
678 fireKey : function(e){
679 //Roo.log('field ' + e.getKey());
680 if(e.isNavKeyPress()){
681 this.fireEvent("specialkey", this, e);
684 focus : function (selectText){
686 this.inputEl().focus();
687 if(selectText === true){
688 this.inputEl().dom.select();
694 onFocus : function(){
695 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
696 // this.el.addClass(this.focusClass);
699 this.hasFocus = true;
700 this.startValue = this.getValue();
701 this.fireEvent("focus", this);
705 beforeBlur : Roo.emptyFn,
711 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
712 //this.el.removeClass(this.focusClass);
714 this.hasFocus = false;
715 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
718 var v = this.getValue();
719 if(String(v) !== String(this.startValue)){
720 this.fireEvent('change', this, v, this.startValue);
722 this.fireEvent("blur", this);
726 * Resets the current field value to the originally loaded value and clears any validation messages
729 this.setValue(this.originalValue);
733 * Returns the name of the field
734 * @return {Mixed} name The name field
740 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
741 * @return {Mixed} value The field value
743 getValue : function(){
745 var v = this.inputEl().getValue();
750 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
751 * @return {Mixed} value The field value
753 getRawValue : function(){
754 var v = this.inputEl().getValue();
760 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
761 * @param {Mixed} value The value to set
763 setRawValue : function(v){
764 return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
767 selectText : function(start, end){
768 var v = this.getRawValue();
770 start = start === undefined ? 0 : start;
771 end = end === undefined ? v.length : end;
772 var d = this.inputEl().dom;
773 if(d.setSelectionRange){
774 d.setSelectionRange(start, end);
775 }else if(d.createTextRange){
776 var range = d.createTextRange();
777 range.moveStart("character", start);
778 range.moveEnd("character", v.length-end);
785 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
786 * @param {Mixed} value The value to set
788 setValue : function(v){
791 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
797 processValue : function(value){
798 if(this.stripCharsRe){
799 var newValue = value.replace(this.stripCharsRe, '');
800 if(newValue !== value){
801 this.setRawValue(newValue);
808 preFocus : function(){
810 if(this.selectOnFocus){
811 this.inputEl().dom.select();
814 filterKeys : function(e){
816 if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
819 var c = e.getCharCode(), cc = String.fromCharCode(c);
820 if(Roo.isIE && (e.isSpecialKey() || !cc)){
823 if(!this.maskRe.test(cc)){
828 * Clear any invalid styles/messages for this field
830 clearInvalid : function(){
832 if(!this.el || this.preventMark){ // not rendered
837 this.indicator.hide();
840 this.el.removeClass(this.invalidClass);
842 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
844 var feedback = this.el.select('.form-control-feedback', true).first();
847 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
852 this.fireEvent('valid', this);
856 * Mark this field as valid
858 markValid : function()
860 if(!this.el || this.preventMark){ // not rendered
864 this.el.removeClass([this.invalidClass, this.validClass]);
866 var feedback = this.el.select('.form-control-feedback', true).first();
869 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
876 if(this.allowBlank && !this.getRawValue().length){
881 this.indicator.hide();
884 this.el.addClass(this.validClass);
886 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
888 var feedback = this.el.select('.form-control-feedback', true).first();
891 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
892 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
897 this.fireEvent('valid', this);
901 * Mark this field as invalid
902 * @param {String} msg The validation message
904 markInvalid : function(msg)
906 if(!this.el || this.preventMark){ // not rendered
910 this.el.removeClass([this.invalidClass, this.validClass]);
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]);
922 if(this.allowBlank && !this.getRawValue().length){
928 this.indicator.show();
932 this.el.addClass(this.invalidClass);
934 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
936 var feedback = this.el.select('.form-control-feedback', true).first();
939 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
941 if(this.getValue().length || this.forceFeedback){
942 this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
949 this.fireEvent('invalid', this, msg);
952 SafariOnKeyDown : function(event)
954 // this is a workaround for a password hang bug on chrome/ webkit.
955 if (this.inputEl().dom.type != 'password') {
959 var isSelectAll = false;
961 if(this.inputEl().dom.selectionEnd > 0){
962 isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
964 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
965 event.preventDefault();
970 if(isSelectAll && event.getCharCode() > 31 && !event.ctrlKey) { // not backspace and delete key (or ctrl-v)
972 event.preventDefault();
973 // this is very hacky as keydown always get's upper case.
975 var cc = String.fromCharCode(event.getCharCode());
976 this.setValue( event.shiftKey ? cc : cc.toLowerCase());
980 adjustWidth : function(tag, w){
981 tag = tag.toLowerCase();
982 if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
983 if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
987 if(tag == 'textarea'){
990 }else if(Roo.isOpera){
994 if(tag == 'textarea'){