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 (0-12)
26 * @cfg {String} labelAlign (top|left)
27 * @cfg {Boolean} readOnly Specifies that the field should be read-only
28 * @cfg {String} autocomplete - default is new-password see: https://developers.google.com/web/fundamentals/input/form/label-and-name-inputs?hl=en
30 * @cfg {String} align (left|center|right) Default left
31 * @cfg {Boolean} forceFeedback (true|false) Default false
38 * @param {Object} config The config object
41 Roo.bootstrap.Input = function(config){
42 Roo.bootstrap.Input.superclass.constructor.call(this, config);
47 * Fires when this field receives input focus.
48 * @param {Roo.form.Field} this
53 * Fires when this field loses input focus.
54 * @param {Roo.form.Field} this
59 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
60 * {@link Roo.EventObject#getKey} to determine which key was pressed.
61 * @param {Roo.form.Field} this
62 * @param {Roo.EventObject} e The event object
67 * Fires just before the field blurs if the field value has changed.
68 * @param {Roo.form.Field} this
69 * @param {Mixed} newValue The new value
70 * @param {Mixed} oldValue The original value
75 * Fires after the field has been marked as invalid.
76 * @param {Roo.form.Field} this
77 * @param {String} msg The validation message
82 * Fires after the field has been validated with no errors.
83 * @param {Roo.form.Field} this
88 * Fires after the key up
89 * @param {Roo.form.Field} this
90 * @param {Roo.EventObject} e The event Object
96 Roo.extend(Roo.bootstrap.Input, Roo.bootstrap.Component, {
98 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
99 automatic validation (defaults to "keyup").
101 validationEvent : "keyup",
103 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
105 validateOnBlur : true,
107 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
109 validationDelay : 250,
111 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
113 focusClass : "x-form-focus", // not needed???
117 * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
119 invalidClass : "has-warning",
122 * @cfg {String} validClass The CSS class to use when marking a field valid (defaults to "x-form-invalid")
124 validClass : "has-success",
127 * @cfg {Boolean} hasFeedback (true|false) default true
132 * @cfg {String} invalidFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
134 invalidFeedbackClass : "glyphicon-warning-sign",
137 * @cfg {String} validFeedbackIcon The CSS class to use when create feedback icon (defaults to "x-form-invalid")
139 validFeedbackClass : "glyphicon-ok",
142 * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives input focus (defaults to false)
144 selectOnFocus : false,
147 * @cfg {String} maskRe An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null)
151 * @cfg {String} vtype A validation type name as defined in {@link Roo.form.VTypes} (defaults to null)
156 * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
158 disableKeyFilter : false,
161 * @cfg {Boolean} disabled True to disable the field (defaults to false).
165 * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
169 * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
171 blankText : "This field is required",
174 * @cfg {Number} minLength Minimum input field length required (defaults to 0)
178 * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
180 maxLength : Number.MAX_VALUE,
182 * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to "The minimum length for this field is {minLength}")
184 minLengthText : "The minimum length for this field is {0}",
186 * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to "The maximum length for this field is {maxLength}")
188 maxLengthText : "The maximum length for this field is {0}",
192 * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
193 * If available, this function will be called only after the basic validators all return true, and will be passed the
194 * current field value and expected to return boolean true if the value is valid or a string error message if invalid.
198 * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
199 * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
200 * current field value. If the test fails, the field will be marked invalid using {@link #regexText}.
204 * @cfg {String} regexText The error text to display if {@link #regex} is used and the test fails during validation (defaults to "")
227 formatedValue : false,
228 forceFeedback : false,
230 parentLabelAlign : function()
233 while (parent.parent()) {
234 parent = parent.parent();
235 if (typeof(parent.labelAlign) !='undefined') {
236 return parent.labelAlign;
243 getAutoCreate : function(){
245 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
251 if(this.inputType != 'hidden'){
252 cfg.cls = 'form-group' //input-group
258 type : this.inputType,
260 cls : 'form-control',
261 placeholder : this.placeholder || '',
262 autocomplete : this.autocomplete || 'new-password'
267 input.style = (typeof(input.style) == 'undefined') ? ('text-align:' + this.align) : (input.style + 'text-align:' + this.align);
270 if(this.maxLength && this.maxLength != Number.MAX_VALUE){
271 input.maxLength = this.maxLength;
283 input.name = this.name;
286 input.cls += ' input-' + this.size;
289 ['xs','sm','md','lg'].map(function(size){
290 if (settings[size]) {
291 cfg.cls += ' col-' + size + '-' + settings[size];
295 var inputblock = input;
299 cls: 'glyphicon form-control-feedback'
302 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
305 cls : 'has-feedback',
313 if (this.before || this.after) {
320 if (this.before && typeof(this.before) == 'string') {
324 cls : 'roo-input-before input-group-addon',
328 if (this.before && typeof(this.before) == 'object') {
329 this.before = Roo.factory(this.before);
333 cls : 'roo-input-before input-group-' +
334 (this.before.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
338 inputblock.cn.push(input);
340 if (this.after && typeof(this.after) == 'string') {
343 cls : 'roo-input-after input-group-addon',
347 if (this.after && typeof(this.after) == 'object') {
348 this.after = Roo.factory(this.after);
352 cls : 'roo-input-after input-group-' +
353 (this.after.xtype == 'Button' ? 'btn' : 'addon') //?? what about checkboxes - that looks like a bit of a hack thought?
357 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
358 inputblock.cls += ' has-feedback';
359 inputblock.cn.push(feedback);
363 if (align ==='left' && this.fieldLabel.length) {
370 cls : 'control-label col-sm-' + this.labelWidth,
371 html : this.fieldLabel
375 cls : "col-sm-" + (12 - this.labelWidth),
382 } else if ( this.fieldLabel.length) {
388 //cls : 'input-group-addon',
389 html : this.fieldLabel
408 if (this.parentType === 'Navbar' && this.parent().bar) {
409 cfg.cls += ' navbar-form';
416 * return the real input element.
420 return this.el.select('input.form-control',true).first();
423 tooltipEl : function()
425 return this.inputEl();
428 setDisabled : function(v)
430 var i = this.inputEl().dom;
432 i.removeAttribute('disabled');
436 i.setAttribute('disabled','true');
438 initEvents : function()
441 this.inputEl().on("keydown" , this.fireKey, this);
442 this.inputEl().on("focus", this.onFocus, this);
443 this.inputEl().on("blur", this.onBlur, this);
445 this.inputEl().relayEvent('keyup', this);
447 // reference to original value for reset
448 this.originalValue = this.getValue();
449 //Roo.form.TextField.superclass.initEvents.call(this);
450 if(this.validationEvent == 'keyup'){
451 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
452 this.inputEl().on('keyup', this.filterValidation, this);
454 else if(this.validationEvent !== false){
455 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
458 if(this.selectOnFocus){
459 this.on("focus", this.preFocus, this);
462 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
463 this.inputEl().on("keypress", this.filterKeys, this);
466 this.el.on("keyup", this.onKeyUp, this, {buffer:50});
467 this.el.on("click", this.autoSize, this);
470 if(this.inputEl().is('input[type=password]') && Roo.isSafari){
471 this.inputEl().on('keydown', this.SafariOnKeyDown, this);
474 if (typeof(this.before) == 'object') {
475 this.before.render(this.el.select('.roo-input-before',true).first());
477 if (typeof(this.after) == 'object') {
478 this.after.render(this.el.select('.roo-input-after',true).first());
483 filterValidation : function(e){
484 if(!e.isNavKeyPress()){
485 this.validationTask.delay(this.validationDelay);
489 * Validates the field value
490 * @return {Boolean} True if the value is valid, else false
492 validate : function(){
493 //if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
494 if(this.disabled || this.validateValue(this.getRawValue())){
505 * Validates a value according to the field's validation rules and marks the field as invalid
506 * if the validation fails
507 * @param {Mixed} value The value to validate
508 * @return {Boolean} True if the value is valid, else false
510 validateValue : function(value){
511 if(value.length < 1) { // if it's blank
518 if(value.length < this.minLength){
521 if(value.length > this.maxLength){
525 var vt = Roo.form.VTypes;
526 if(!vt[this.vtype](value, this)){
530 if(typeof this.validator == "function"){
531 var msg = this.validator(value);
537 if(this.regex && !this.regex.test(value)){
547 fireKey : function(e){
548 //Roo.log('field ' + e.getKey());
549 if(e.isNavKeyPress()){
550 this.fireEvent("specialkey", this, e);
553 focus : function (selectText){
555 this.inputEl().focus();
556 if(selectText === true){
557 this.inputEl().dom.select();
563 onFocus : function(){
564 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
565 // this.el.addClass(this.focusClass);
568 this.hasFocus = true;
569 this.startValue = this.getValue();
570 this.fireEvent("focus", this);
574 beforeBlur : Roo.emptyFn,
580 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
581 //this.el.removeClass(this.focusClass);
583 this.hasFocus = false;
584 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
587 var v = this.getValue();
588 if(String(v) !== String(this.startValue)){
589 this.fireEvent('change', this, v, this.startValue);
591 this.fireEvent("blur", this);
595 * Resets the current field value to the originally loaded value and clears any validation messages
598 this.setValue(this.originalValue);
602 * Returns the name of the field
603 * @return {Mixed} name The name field
609 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
610 * @return {Mixed} value The field value
612 getValue : function(){
614 var v = this.inputEl().getValue();
619 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
620 * @return {Mixed} value The field value
622 getRawValue : function(){
623 var v = this.inputEl().getValue();
629 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
630 * @param {Mixed} value The value to set
632 setRawValue : function(v){
633 return this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
636 selectText : function(start, end){
637 var v = this.getRawValue();
639 start = start === undefined ? 0 : start;
640 end = end === undefined ? v.length : end;
641 var d = this.inputEl().dom;
642 if(d.setSelectionRange){
643 d.setSelectionRange(start, end);
644 }else if(d.createTextRange){
645 var range = d.createTextRange();
646 range.moveStart("character", start);
647 range.moveEnd("character", v.length-end);
654 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
655 * @param {Mixed} value The value to set
657 setValue : function(v){
660 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
666 processValue : function(value){
667 if(this.stripCharsRe){
668 var newValue = value.replace(this.stripCharsRe, '');
669 if(newValue !== value){
670 this.setRawValue(newValue);
677 preFocus : function(){
679 if(this.selectOnFocus){
680 this.inputEl().dom.select();
683 filterKeys : function(e){
685 if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
688 var c = e.getCharCode(), cc = String.fromCharCode(c);
689 if(Roo.isIE && (e.isSpecialKey() || !cc)){
692 if(!this.maskRe.test(cc)){
697 * Clear any invalid styles/messages for this field
699 clearInvalid : function(){
701 if(!this.el || this.preventMark){ // not rendered
704 this.el.removeClass(this.invalidClass);
706 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
708 var feedback = this.el.select('.form-control-feedback', true).first();
711 this.el.select('.form-control-feedback', true).first().removeClass(this.invalidFeedbackClass);
716 this.fireEvent('valid', this);
720 * Mark this field as valid
722 markValid : function()
724 if(!this.el || this.preventMark){ // not rendered
728 this.el.removeClass([this.invalidClass, this.validClass]);
730 var feedback = this.el.select('.form-control-feedback', true).first();
733 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
736 if(this.disabled || this.allowBlank){
740 var formGroup = this.el.findParent('.form-group', false, true);
744 var label = formGroup.select('label', true).first();
745 var icon = formGroup.select('i.fa-star', true).first();
752 this.el.addClass(this.validClass);
754 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
756 var feedback = this.el.select('.form-control-feedback', true).first();
759 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
760 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
765 this.fireEvent('valid', this);
769 * Mark this field as invalid
770 * @param {String} msg The validation message
772 markInvalid : function(msg)
774 if(!this.el || this.preventMark){ // not rendered
778 this.el.removeClass([this.invalidClass, this.validClass]);
780 var feedback = this.el.select('.form-control-feedback', true).first();
783 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
786 if(this.disabled || this.allowBlank){
790 var formGroup = this.el.findParent('.form-group', false, true);
793 var label = formGroup.select('label', true).first();
794 var icon = formGroup.select('i.fa-star', true).first();
796 if(!this.getValue().length && label && !icon){
797 this.el.findParent('.form-group', false, true).createChild({
799 cls : 'text-danger fa fa-lg fa-star',
800 tooltip : 'This field is required',
801 style : 'margin-right:5px;'
807 this.el.addClass(this.invalidClass);
809 if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
811 var feedback = this.el.select('.form-control-feedback', true).first();
814 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
816 if(this.getValue().length || this.forceFeedback){
817 this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
824 this.fireEvent('invalid', this, msg);
827 SafariOnKeyDown : function(event)
829 // this is a workaround for a password hang bug on chrome/ webkit.
831 var isSelectAll = false;
833 if(this.inputEl().dom.selectionEnd > 0){
834 isSelectAll = (this.inputEl().dom.selectionEnd - this.inputEl().dom.selectionStart - this.getValue().length == 0) ? true : false;
836 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
837 event.preventDefault();
842 if(isSelectAll && event.getCharCode() > 31){ // not backspace and delete key
844 event.preventDefault();
845 // this is very hacky as keydown always get's upper case.
847 var cc = String.fromCharCode(event.getCharCode());
848 this.setValue( event.shiftKey ? cc : cc.toLowerCase());
852 adjustWidth : function(tag, w){
853 tag = tag.toLowerCase();
854 if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
855 if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
859 if(tag == 'textarea'){
862 }else if(Roo.isOpera){
866 if(tag == 'textarea'){