3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.form.Field
9 * @extends Ext.BoxComponent
10 * Base class for form fields that provides default event handling, sizing, value handling and other functionality.
13 * @param {Object} config Configuration options
16 Ext.form.Field = Ext.extend(Ext.BoxComponent, {
18 * @cfg {String} inputType The type attribute for input fields -- e.g. radio, text, password, file (defaults
19 * to "text"). The types "file" and "password" must be used to render those field types currently -- there are
20 * no separate Ext components for those. Note that if you use <tt>inputType:'file'</tt>, {@link #emptyText}
21 * is not supported and should be avoided.
24 * @cfg {Number} tabIndex The tabIndex for this field. Note this only applies to fields that are rendered,
25 * not those which are built via applyTo (defaults to undefined).
28 * @cfg {Mixed} value A value to initialize this field with (defaults to undefined).
31 * @cfg {String} name The field's HTML name attribute (defaults to "").
32 * <b>Note</b>: this property must be set if this field is to be automatically included with
33 * {@link Ext.form.BasicForm#submit form submit()}.
36 * @cfg {String} cls A custom CSS class to apply to the field's underlying element (defaults to "").
40 * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
42 invalidClass : "x-form-invalid",
44 * @cfg {String} invalidText The error text to use when marking a field invalid and no message is provided
45 * (defaults to "The value in this field is invalid")
47 invalidText : "The value in this field is invalid",
49 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
51 focusClass : "x-form-focus",
53 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
54 automatic validation (defaults to "keyup").
56 validationEvent : "keyup",
58 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
60 validateOnBlur : true,
62 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation
63 * is initiated (defaults to 250)
65 validationDelay : 250,
67 * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or true for a default
68 * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.
69 * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details. Defaults to:</p>
70 * <pre><code>{tag: "input", type: "text", size: "20", autocomplete: "off"}</code></pre>
72 defaultAutoCreate : {tag: "input", type: "text", size: "20", autocomplete: "off"},
74 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field")
76 fieldClass : "x-form-field",
78 * @cfg {String} msgTarget The location where error text should display. Should be one of the following values
79 * (defaults to 'qtip'):
82 ----------- ----------------------------------------------------------------------
83 qtip Display a quick tip when the user hovers over the field
84 title Display a default browser title attribute popup
85 under Add a block div beneath the field containing the error text
86 side Add an error icon to the right of the field with a popup on hover
87 [element id] Add the error text directly to the innerHTML of the specified element
92 * @cfg {String} msgFx <b>Experimental</b> The effect used when displaying a validation message under the field
93 * (defaults to 'normal').
97 * @cfg {Boolean} readOnly <tt>true</tt> to mark the field as readOnly in HTML
98 * (defaults to <tt>false</tt>).
99 * <br><p><b>Note</b>: this only sets the element's readOnly DOM attribute.
100 * Setting <code>readOnly=true</code>, for example, will not disable triggering a
101 * ComboBox or DateField; it gives you the option of forcing the user to choose
102 * via the trigger without typing in the text box. To hide the trigger use
103 * <code>{@link Ext.form.TriggerField#hideTrigger hideTrigger}</code>.</p>
107 * @cfg {Boolean} disabled True to disable the field (defaults to false).
108 * <p>Be aware that conformant with the <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1">HTML specification</a>,
109 * disabled Fields will not be {@link Ext.form.BasicForm#submit submitted}.</p>
120 initComponent : function(){
121 Ext.form.Field.superclass.initComponent.call(this);
125 * Fires when this field receives input focus.
126 * @param {Ext.form.Field} this
131 * Fires when this field loses input focus.
132 * @param {Ext.form.Field} this
137 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.
138 * To handle other keys see {@link Ext.Panel#keys} or {@link Ext.KeyMap}.
139 * You can check {@link Ext.EventObject#getKey} to determine which key was pressed.
140 * For example: <pre><code>
141 var form = new Ext.form.FormPanel({
144 fieldLabel: 'Field 1',
148 fieldLabel: 'Field 2',
151 specialkey: function(field, e){
152 // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
153 // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
154 if (e.{@link Ext.EventObject#getKey getKey()} == e.ENTER) {
155 var form = field.ownerCt.getForm();
165 * @param {Ext.form.Field} this
166 * @param {Ext.EventObject} e The event object
171 * Fires just before the field blurs if the field value has changed.
172 * @param {Ext.form.Field} this
173 * @param {Mixed} newValue The new value
174 * @param {Mixed} oldValue The original value
179 * Fires after the field has been marked as invalid.
180 * @param {Ext.form.Field} this
181 * @param {String} msg The validation message
186 * Fires after the field has been validated with no errors.
187 * @param {Ext.form.Field} this
194 * Returns the {@link Ext.form.Field#name name} or {@link Ext.form.ComboBox#hiddenName hiddenName}
195 * attribute of the field if available.
196 * @return {String} name The field {@link Ext.form.Field#name name} or {@link Ext.form.ComboBox#hiddenName hiddenName}
199 return this.rendered && this.el.dom.name ? this.el.dom.name : this.name || this.id || '';
203 onRender : function(ct, position){
205 var cfg = this.getAutoCreate();
208 cfg.name = this.name || this.id;
211 cfg.type = this.inputType;
215 Ext.form.Field.superclass.onRender.call(this, ct, position);
217 var type = this.el.dom.type;
219 if(type == 'password'){
222 this.el.addClass('x-form-'+type);
225 this.el.dom.readOnly = true;
227 if(this.tabIndex !== undefined){
228 this.el.dom.setAttribute('tabIndex', this.tabIndex);
231 this.el.addClass([this.fieldClass, this.cls]);
235 getItemCt : function(){
236 return this.el.up('.x-form-item', 4);
240 initValue : function(){
241 if(this.value !== undefined){
242 this.setValue(this.value);
243 }else if(!Ext.isEmpty(this.el.dom.value) && this.el.dom.value != this.emptyText){
244 this.setValue(this.el.dom.value);
247 * The original value of the field as configured in the {@link #value} configuration, or
248 * as loaded by the last form load operation if the form's {@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
249 * setting is <code>true</code>.
251 * @property originalValue
253 this.originalValue = this.getValue();
257 * <p>Returns true if the value of this Field has been changed from its original value.
258 * Will return false if the field is disabled or has not been rendered yet.</p>
259 * <p>Note that if the owning {@link Ext.form.BasicForm form} was configured with
260 * {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
261 * then the <i>original value</i> is updated when the values are loaded by
262 * {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#setValues setValues}.</p>
263 * @return {Boolean} True if this field has been changed from its original value (and
264 * is not disabled), false otherwise.
266 isDirty : function() {
267 if(this.disabled || !this.rendered) {
270 return String(this.getValue()) !== String(this.originalValue);
274 afterRender : function(){
275 Ext.form.Field.superclass.afterRender.call(this);
281 fireKey : function(e){
282 if(e.isSpecialKey()){
283 this.fireEvent("specialkey", this, e);
288 * Resets the current field value to the originally loaded value and clears any validation messages.
289 * See {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
292 this.setValue(this.originalValue);
297 initEvents : function(){
298 this.mon(this.el, Ext.EventManager.useKeydown ? "keydown" : "keypress", this.fireKey, this);
299 this.mon(this.el, 'focus', this.onFocus, this);
301 // fix weird FF/Win editor issue when changing OS window focus
302 var o = this.inEditor && Ext.isWindows && Ext.isGecko ? {buffer:10} : null;
303 this.mon(this.el, 'blur', this.onBlur, this, o);
307 onFocus : function(){
309 this.el.addClass(this.focusClass);
312 this.hasFocus = true;
313 this.startValue = this.getValue();
314 this.fireEvent("focus", this);
319 beforeBlur : Ext.emptyFn,
325 this.el.removeClass(this.focusClass);
327 this.hasFocus = false;
328 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
331 var v = this.getValue();
332 if(String(v) !== String(this.startValue)){
333 this.fireEvent('change', this, v, this.startValue);
335 this.fireEvent("blur", this);
339 * Returns whether or not the field value is currently valid
340 * @param {Boolean} preventMark True to disable marking the field invalid
341 * @return {Boolean} True if the value is valid, else false
343 isValid : function(preventMark){
347 var restore = this.preventMark;
348 this.preventMark = preventMark === true;
349 var v = this.validateValue(this.processValue(this.getRawValue()));
350 this.preventMark = restore;
355 * Validates the field value
356 * @return {Boolean} True if the value is valid, else false
358 validate : function(){
359 if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
366 // protected - should be overridden by subclasses if necessary to prepare raw values for validation
367 processValue : function(value){
372 // Subclasses should provide the validation implementation by overriding this
373 validateValue : function(value){
378 * Mark this field as invalid, using {@link #msgTarget} to determine how to display the error and
379 * applying {@link #invalidClass} to the field's element.
380 * @param {String} msg (optional) The validation message (defaults to {@link #invalidText})
382 markInvalid : function(msg){
383 if(!this.rendered || this.preventMark){ // not rendered
386 msg = msg || this.invalidText;
388 var mt = this.getMessageHandler();
391 }else if(this.msgTarget){
392 this.el.addClass(this.invalidClass);
393 var t = Ext.getDom(this.msgTarget);
396 t.style.display = this.msgDisplay;
399 this.fireEvent('invalid', this, msg);
403 * Clear any invalid styles/messages for this field
405 clearInvalid : function(){
406 if(!this.rendered || this.preventMark){ // not rendered
409 this.el.removeClass(this.invalidClass);
410 var mt = this.getMessageHandler();
413 }else if(this.msgTarget){
414 this.el.removeClass(this.invalidClass);
415 var t = Ext.getDom(this.msgTarget);
418 t.style.display = 'none';
421 this.fireEvent('valid', this);
425 getMessageHandler : function(){
426 return Ext.form.MessageTargets[this.msgTarget];
430 getErrorCt : function(){
431 return this.el.findParent('.x-form-element', 5, true) || // use form element wrap if available
432 this.el.findParent('.x-form-field-wrap', 5, true); // else direct field wrap
436 alignErrorIcon : function(){
437 this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
441 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
442 * @return {Mixed} value The field value
444 getRawValue : function(){
445 var v = this.rendered ? this.el.getValue() : Ext.value(this.value, '');
446 if(v === this.emptyText){
453 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
454 * @return {Mixed} value The field value
456 getValue : function(){
460 var v = this.el.getValue();
461 if(v === this.emptyText || v === undefined){
468 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
469 * @param {Mixed} value The value to set
470 * @return {Mixed} value The field value that is set
472 setRawValue : function(v){
473 return (this.el.dom.value = (Ext.isEmpty(v) ? '' : v));
477 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
478 * @param {Mixed} value The value to set
479 * @return {Ext.form.Field} this
481 setValue : function(v){
484 this.el.dom.value = (Ext.isEmpty(v) ? '' : v);
490 // private, does not work for all fields
491 append : function(v){
492 this.setValue([this.getValue(), v].join(''));
496 adjustSize : function(w, h){
497 var s = Ext.form.Field.superclass.adjustSize.call(this, w, h);
498 s.width = this.adjustWidth(this.el.dom.tagName, s.width);
500 var ct = this.getItemCt();
501 s.width -= ct.getFrameWidth('lr');
502 s.height -= ct.getFrameWidth('tb');
508 adjustWidth : function(tag, w){
509 if(typeof w == 'number' && (Ext.isIE && (Ext.isIE6 || !Ext.isStrict)) && /input|textarea/i.test(tag) && !this.inEditor){
516 * @cfg {Boolean} autoWidth @hide
519 * @cfg {Boolean} autoHeight @hide
523 * @cfg {String} autoEl @hide
528 Ext.form.MessageTargets = {
530 mark: function(field, msg){
531 field.el.addClass(field.invalidClass);
532 field.el.dom.qtip = msg;
533 field.el.dom.qclass = 'x-form-invalid-tip';
534 if(Ext.QuickTips){ // fix for floating editors interacting with DND
535 Ext.QuickTips.enable();
538 clear: function(field){
539 field.el.removeClass(field.invalidClass);
540 field.el.dom.qtip = '';
544 mark: function(field, msg){
545 field.el.addClass(field.invalidClass);
546 field.el.dom.title = msg;
548 clear: function(field){
549 field.el.dom.title = '';
553 mark: function(field, msg){
554 field.el.addClass(field.invalidClass);
556 var elp = field.getErrorCt();
557 if(!elp){ // field has no container el
558 field.el.dom.title = msg;
561 field.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
562 field.errorEl.setWidth(elp.getWidth(true)-20);
564 field.errorEl.update(msg);
565 Ext.form.Field.msgFx[field.msgFx].show(field.errorEl, field);
567 clear: function(field){
568 field.el.removeClass(field.invalidClass);
570 Ext.form.Field.msgFx[field.msgFx].hide(field.errorEl, field);
572 field.el.dom.title = '';
577 mark: function(field, msg){
578 field.el.addClass(field.invalidClass);
579 if(!field.errorIcon){
580 var elp = field.getErrorCt();
581 if(!elp){ // field has no container el
582 field.el.dom.title = msg;
585 field.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
587 field.alignErrorIcon();
588 field.errorIcon.dom.qtip = msg;
589 field.errorIcon.dom.qclass = 'x-form-invalid-tip';
590 field.errorIcon.show();
591 field.on('resize', field.alignErrorIcon, field);
593 clear: function(field){
594 field.el.removeClass(field.invalidClass);
596 field.errorIcon.dom.qtip = '';
597 field.errorIcon.hide();
598 field.un('resize', field.alignErrorIcon, field);
600 field.el.dom.title = '';
606 // anything other than normal should be considered experimental
607 Ext.form.Field.msgFx = {
609 show: function(msgEl, f){
610 msgEl.setDisplayed('block');
613 hide : function(msgEl, f){
614 msgEl.setDisplayed(false).update('');
619 show: function(msgEl, f){
620 msgEl.slideIn('t', {stopFx:true});
623 hide : function(msgEl, f){
624 msgEl.slideOut('t', {stopFx:true,useDisplay:true});
629 show: function(msgEl, f){
631 msgEl.alignTo(f.el, 'tl-tr');
632 msgEl.slideIn('l', {stopFx:true});
635 hide : function(msgEl, f){
636 msgEl.slideOut('l', {stopFx:true,useDisplay:true});
640 Ext.reg('field', Ext.form.Field);