3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.form.Field"></div>/**
10 * @class Ext.form.Field
11 * @extends Ext.BoxComponent
12 * Base class for form fields that provides default event handling, sizing, value handling and other functionality.
15 * @param {Object} config Configuration options
18 Ext.form.Field = Ext.extend(Ext.BoxComponent, {
19 <div id="prop-Ext.form.Field-label"></div>/**
20 * <p>The label Element associated with this Field. <b>Only available after this Field has been rendered by a
21 * {@link form Ext.layout.FormLayout} layout manager.</b></p>
25 <div id="cfg-Ext.form.Field-inputType"></div>/**
26 * @cfg {String} inputType The type attribute for input fields -- e.g. radio, text, password, file (defaults
27 * to 'text'). The types 'file' and 'password' must be used to render those field types currently -- there are
28 * no separate Ext components for those. Note that if you use <tt>inputType:'file'</tt>, {@link #emptyText}
29 * is not supported and should be avoided.
31 <div id="cfg-Ext.form.Field-tabIndex"></div>/**
32 * @cfg {Number} tabIndex The tabIndex for this field. Note this only applies to fields that are rendered,
33 * not those which are built via applyTo (defaults to undefined).
35 <div id="cfg-Ext.form.Field-value"></div>/**
36 * @cfg {Mixed} value A value to initialize this field with (defaults to undefined).
38 <div id="cfg-Ext.form.Field-name"></div>/**
39 * @cfg {String} name The field's HTML name attribute (defaults to '').
40 * <b>Note</b>: this property must be set if this field is to be automatically included with
41 * {@link Ext.form.BasicForm#submit form submit()}.
43 <div id="cfg-Ext.form.Field-cls"></div>/**
44 * @cfg {String} cls A custom CSS class to apply to the field's underlying element (defaults to '').
47 <div id="cfg-Ext.form.Field-invalidClass"></div>/**
48 * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to 'x-form-invalid')
50 invalidClass : 'x-form-invalid',
51 <div id="cfg-Ext.form.Field-invalidText"></div>/**
52 * @cfg {String} invalidText The error text to use when marking a field invalid and no message is provided
53 * (defaults to 'The value in this field is invalid')
55 invalidText : 'The value in this field is invalid',
56 <div id="cfg-Ext.form.Field-focusClass"></div>/**
57 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to 'x-form-focus')
59 focusClass : 'x-form-focus',
60 <div id="cfg-Ext.form.Field-preventMark"></div>/**
61 * @cfg {Boolean} preventMark
62 * <tt>true</tt> to disable {@link #markInvalid marking the field invalid}.
63 * Defaults to <tt>false</tt>.
65 <div id="cfg-Ext.form.Field-validationEvent"></div>/**
66 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
67 automatic validation (defaults to 'keyup').
69 validationEvent : 'keyup',
70 <div id="cfg-Ext.form.Field-validateOnBlur"></div>/**
71 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
73 validateOnBlur : true,
74 <div id="cfg-Ext.form.Field-validationDelay"></div>/**
75 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation
76 * is initiated (defaults to 250)
78 validationDelay : 250,
79 <div id="cfg-Ext.form.Field-autoCreate"></div>/**
80 * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or true for a default
81 * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.
82 * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details. Defaults to:</p>
83 * <pre><code>{tag: 'input', type: 'text', size: '20', autocomplete: 'off'}</code></pre>
85 defaultAutoCreate : {tag: 'input', type: 'text', size: '20', autocomplete: 'off'},
86 <div id="cfg-Ext.form.Field-fieldClass"></div>/**
87 * @cfg {String} fieldClass The default CSS class for the field (defaults to 'x-form-field')
89 fieldClass : 'x-form-field',
90 <div id="cfg-Ext.form.Field-msgTarget<p>The"></div>/**
91 * @cfg {String} msgTarget<p>The location where the message text set through {@link #markInvalid} should display.
92 * Must be one of the following values:</p>
93 * <div class="mdetail-params"><ul>
94 * <li><code>qtip</code> Display a quick tip containing the message when the user hovers over the field. This is the default.
95 * <div class="subdesc"><b>{@link Ext.QuickTips#init Ext.QuickTips.init} must have been called for this setting to work.</b></div</li>
96 * <li><code>title</code> Display the message in a default browser title attribute popup.</li>
97 * <li><code>under</code> Add a block div beneath the field containing the error message.</li>
98 * <li><code>side</code> Add an error icon to the right of the field, displaying the message in a popup on hover.</li>
99 * <li><code>[element id]</code> Add the error message directly to the innerHTML of the specified element.</li>
103 <div id="cfg-Ext.form.Field-msgFx"></div>/**
104 * @cfg {String} msgFx <b>Experimental</b> The effect used when displaying a validation message under the field
105 * (defaults to 'normal').
108 <div id="cfg-Ext.form.Field-readOnly"></div>/**
109 * @cfg {Boolean} readOnly <tt>true</tt> to mark the field as readOnly in HTML
110 * (defaults to <tt>false</tt>).
111 * <br><p><b>Note</b>: this only sets the element's readOnly DOM attribute.
112 * Setting <code>readOnly=true</code>, for example, will not disable triggering a
113 * ComboBox or DateField; it gives you the option of forcing the user to choose
114 * via the trigger without typing in the text box. To hide the trigger use
115 * <code>{@link Ext.form.TriggerField#hideTrigger hideTrigger}</code>.</p>
118 <div id="cfg-Ext.form.Field-disabled"></div>/**
119 * @cfg {Boolean} disabled True to disable the field (defaults to false).
120 * <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>,
121 * disabled Fields will not be {@link Ext.form.BasicForm#submit submitted}.</p>
124 <div id="cfg-Ext.form.Field-submitValue"></div>/**
125 * @cfg {Boolean} submitValue False to clear the name attribute on the field so that it is not submitted during a form post.
126 * Defaults to <tt>true</tt>.
140 initComponent : function(){
141 Ext.form.Field.superclass.initComponent.call(this);
143 <div id="event-Ext.form.Field-focus"></div>/**
145 * Fires when this field receives input focus.
146 * @param {Ext.form.Field} this
149 <div id="event-Ext.form.Field-blur"></div>/**
151 * Fires when this field loses input focus.
152 * @param {Ext.form.Field} this
155 <div id="event-Ext.form.Field-specialkey"></div>/**
157 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.
158 * To handle other keys see {@link Ext.Panel#keys} or {@link Ext.KeyMap}.
159 * You can check {@link Ext.EventObject#getKey} to determine which key was pressed.
160 * For example: <pre><code>
161 var form = new Ext.form.FormPanel({
164 fieldLabel: 'Field 1',
168 fieldLabel: 'Field 2',
171 specialkey: function(field, e){
172 // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
173 // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
174 if (e.{@link Ext.EventObject#getKey getKey()} == e.ENTER) {
175 var form = field.ownerCt.getForm();
185 * @param {Ext.form.Field} this
186 * @param {Ext.EventObject} e The event object
189 <div id="event-Ext.form.Field-change"></div>/**
191 * Fires just before the field blurs if the field value has changed.
192 * @param {Ext.form.Field} this
193 * @param {Mixed} newValue The new value
194 * @param {Mixed} oldValue The original value
197 <div id="event-Ext.form.Field-invalid"></div>/**
199 * Fires after the field has been marked as invalid.
200 * @param {Ext.form.Field} this
201 * @param {String} msg The validation message
204 <div id="event-Ext.form.Field-valid"></div>/**
206 * Fires after the field has been validated with no errors.
207 * @param {Ext.form.Field} this
213 <div id="method-Ext.form.Field-getName"></div>/**
214 * Returns the {@link Ext.form.Field#name name} or {@link Ext.form.ComboBox#hiddenName hiddenName}
215 * attribute of the field if available.
216 * @return {String} name The field {@link Ext.form.Field#name name} or {@link Ext.form.ComboBox#hiddenName hiddenName}
218 getName : function(){
219 return this.rendered && this.el.dom.name ? this.el.dom.name : this.name || this.id || '';
223 onRender : function(ct, position){
225 var cfg = this.getAutoCreate();
228 cfg.name = this.name || this.id;
231 cfg.type = this.inputType;
235 Ext.form.Field.superclass.onRender.call(this, ct, position);
236 if(this.submitValue === false){
237 this.el.dom.removeAttribute('name');
239 var type = this.el.dom.type;
241 if(type == 'password'){
244 this.el.addClass('x-form-'+type);
247 this.setReadOnly(true);
249 if(this.tabIndex !== undefined){
250 this.el.dom.setAttribute('tabIndex', this.tabIndex);
253 this.el.addClass([this.fieldClass, this.cls]);
257 getItemCt : function(){
262 initValue : function(){
263 if(this.value !== undefined){
264 this.setValue(this.value);
265 }else if(!Ext.isEmpty(this.el.dom.value) && this.el.dom.value != this.emptyText){
266 this.setValue(this.el.dom.value);
268 <div id="prop-Ext.form.Field-originalValue"></div>/**
269 * The original value of the field as configured in the {@link #value} configuration, or
270 * as loaded by the last form load operation if the form's {@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
271 * setting is <code>true</code>.
273 * @property originalValue
275 this.originalValue = this.getValue();
278 <div id="method-Ext.form.Field-isDirty"></div>/**
279 * <p>Returns true if the value of this Field has been changed from its original value.
280 * Will return false if the field is disabled or has not been rendered yet.</p>
281 * <p>Note that if the owning {@link Ext.form.BasicForm form} was configured with
282 * {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
283 * then the <i>original value</i> is updated when the values are loaded by
284 * {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#setValues setValues}.</p>
285 * @return {Boolean} True if this field has been changed from its original value (and
286 * is not disabled), false otherwise.
288 isDirty : function() {
289 if(this.disabled || !this.rendered) {
292 return String(this.getValue()) !== String(this.originalValue);
295 <div id="method-Ext.form.Field-setReadOnly"></div>/**
296 * Sets the read only state of this field.
297 * @param {Boolean} readOnly Whether the field should be read only.
299 setReadOnly : function(readOnly){
301 this.el.dom.readOnly = readOnly;
303 this.readOnly = readOnly;
307 afterRender : function(){
308 Ext.form.Field.superclass.afterRender.call(this);
314 fireKey : function(e){
315 if(e.isSpecialKey()){
316 this.fireEvent('specialkey', this, e);
320 <div id="method-Ext.form.Field-reset"></div>/**
321 * Resets the current field value to the originally loaded value and clears any validation messages.
322 * See {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
325 this.setValue(this.originalValue);
330 initEvents : function(){
331 this.mon(this.el, Ext.EventManager.useKeydown ? 'keydown' : 'keypress', this.fireKey, this);
332 this.mon(this.el, 'focus', this.onFocus, this);
334 // standardise buffer across all browsers + OS-es for consistent event order.
335 // (the 10ms buffer for Editors fixes a weird FF/Win editor issue when changing OS window focus)
336 this.mon(this.el, 'blur', this.onBlur, this, this.inEditor ? {buffer:10} : null);
340 preFocus: Ext.emptyFn,
343 onFocus : function(){
346 this.el.addClass(this.focusClass);
349 this.hasFocus = true;
350 <div id="prop-Ext.form.Field-startValue"></div>/**
351 * <p>The value that the Field had at the time it was last focused. This is the value that is passed
352 * to the {@link #change} event which is fired if the value has been changed when the Field is blurred.</p>
353 * <p><b>This will be undefined until the Field has been visited.</b> Compare {@link #originalValue}.</p>
355 * @property startValue
357 this.startValue = this.getValue();
358 this.fireEvent('focus', this);
363 beforeBlur : Ext.emptyFn,
369 this.el.removeClass(this.focusClass);
371 this.hasFocus = false;
372 if(this.validationEvent !== false && (this.validateOnBlur || this.validationEvent == 'blur')){
375 var v = this.getValue();
376 if(String(v) !== String(this.startValue)){
377 this.fireEvent('change', this, v, this.startValue);
379 this.fireEvent('blur', this);
384 postBlur : Ext.emptyFn,
386 <div id="method-Ext.form.Field-isValid"></div>/**
387 * Returns whether or not the field value is currently valid by
388 * {@link #validateValue validating} the {@link #processValue processed value}
389 * of the field. <b>Note</b>: {@link #disabled} fields are ignored.
390 * @param {Boolean} preventMark True to disable marking the field invalid
391 * @return {Boolean} True if the value is valid, else false
393 isValid : function(preventMark){
397 var restore = this.preventMark;
398 this.preventMark = preventMark === true;
399 var v = this.validateValue(this.processValue(this.getRawValue()));
400 this.preventMark = restore;
404 <div id="method-Ext.form.Field-validate"></div>/**
405 * Validates the field value
406 * @return {Boolean} True if the value is valid, else false
408 validate : function(){
409 if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
416 <div id="method-Ext.form.Field-processValue"></div>/**
417 * This method should only be overridden if necessary to prepare raw values
418 * for validation (see {@link #validate} and {@link #isValid}). This method
419 * is expected to return the processed value for the field which will
420 * be used for validation (see validateValue method).
421 * @param {Mixed} value
423 processValue : function(value){
429 * Subclasses should provide the validation implementation by overriding this
430 * @param {Mixed} value
432 validateValue : function(value){
436 <div id="method-Ext.form.Field-getActiveError"></div>/**
437 * Gets the active error message for this field.
438 * @return {String} Returns the active error message on the field, if there is no error, an empty string is returned.
440 getActiveError : function(){
441 return this.activeError || '';
444 <div id="method-Ext.form.Field-markInvalid"></div>/**
445 * <p>Display an error message associated with this field, using {@link #msgTarget} to determine how to
446 * display the message and applying {@link #invalidClass} to the field's UI element.</p>
447 * <p><b>Note</b>: this method does not cause the Field's {@link #validate} method to return <code>false</code>
448 * if the value does <i>pass</i> validation. So simply marking a Field as invalid will not prevent
449 * submission of forms submitted with the {@link Ext.form.Action.Submit#clientValidation} option set.</p>
450 * {@link #isValid invalid}.
451 * @param {String} msg (optional) The validation message (defaults to {@link #invalidText})
453 markInvalid : function(msg){
454 if(!this.rendered || this.preventMark){ // not rendered
457 msg = msg || this.invalidText;
459 var mt = this.getMessageHandler();
462 }else if(this.msgTarget){
463 this.el.addClass(this.invalidClass);
464 var t = Ext.getDom(this.msgTarget);
467 t.style.display = this.msgDisplay;
470 this.activeError = msg;
471 this.fireEvent('invalid', this, msg);
474 <div id="method-Ext.form.Field-clearInvalid"></div>/**
475 * Clear any invalid styles/messages for this field
477 clearInvalid : function(){
478 if(!this.rendered || this.preventMark){ // not rendered
481 this.el.removeClass(this.invalidClass);
482 var mt = this.getMessageHandler();
485 }else if(this.msgTarget){
486 this.el.removeClass(this.invalidClass);
487 var t = Ext.getDom(this.msgTarget);
490 t.style.display = 'none';
493 delete this.activeError;
494 this.fireEvent('valid', this);
498 getMessageHandler : function(){
499 return Ext.form.MessageTargets[this.msgTarget];
503 getErrorCt : function(){
504 return this.el.findParent('.x-form-element', 5, true) || // use form element wrap if available
505 this.el.findParent('.x-form-field-wrap', 5, true); // else direct field wrap
508 // Alignment for 'under' target
509 alignErrorEl : function(){
510 this.errorEl.setWidth(this.getErrorCt().getWidth(true) - 20);
513 // Alignment for 'side' target
514 alignErrorIcon : function(){
515 this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
518 <div id="method-Ext.form.Field-getRawValue"></div>/**
519 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
520 * @return {Mixed} value The field value
522 getRawValue : function(){
523 var v = this.rendered ? this.el.getValue() : Ext.value(this.value, '');
524 if(v === this.emptyText){
530 <div id="method-Ext.form.Field-getValue"></div>/**
531 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
532 * @return {Mixed} value The field value
534 getValue : function(){
538 var v = this.el.getValue();
539 if(v === this.emptyText || v === undefined){
545 <div id="method-Ext.form.Field-setRawValue"></div>/**
546 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
547 * @param {Mixed} value The value to set
548 * @return {Mixed} value The field value that is set
550 setRawValue : function(v){
551 return this.rendered ? (this.el.dom.value = (Ext.isEmpty(v) ? '' : v)) : '';
554 <div id="method-Ext.form.Field-setValue"></div>/**
555 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
556 * @param {Mixed} value The value to set
557 * @return {Ext.form.Field} this
559 setValue : function(v){
562 this.el.dom.value = (Ext.isEmpty(v) ? '' : v);
568 // private, does not work for all fields
569 append : function(v){
570 this.setValue([this.getValue(), v].join(''));
573 <div id="cfg-Ext.form.Field-autoWidth"></div>/**
574 * @cfg {Boolean} autoWidth @hide
576 <div id="cfg-Ext.form.Field-autoHeight"></div>/**
577 * @cfg {Boolean} autoHeight @hide
580 <div id="cfg-Ext.form.Field-autoEl"></div>/**
581 * @cfg {String} autoEl @hide
586 Ext.form.MessageTargets = {
588 mark: function(field, msg){
589 field.el.addClass(field.invalidClass);
590 field.el.dom.qtip = msg;
591 field.el.dom.qclass = 'x-form-invalid-tip';
592 if(Ext.QuickTips){ // fix for floating editors interacting with DND
593 Ext.QuickTips.enable();
596 clear: function(field){
597 field.el.removeClass(field.invalidClass);
598 field.el.dom.qtip = '';
602 mark: function(field, msg){
603 field.el.addClass(field.invalidClass);
604 field.el.dom.title = msg;
606 clear: function(field){
607 field.el.dom.title = '';
611 mark: function(field, msg){
612 field.el.addClass(field.invalidClass);
614 var elp = field.getErrorCt();
615 if(!elp){ // field has no container el
616 field.el.dom.title = msg;
619 field.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
620 field.on('resize', field.alignErrorEl, field);
621 field.on('destroy', function(){
622 Ext.destroy(this.errorEl);
625 field.alignErrorEl();
626 field.errorEl.update(msg);
627 Ext.form.Field.msgFx[field.msgFx].show(field.errorEl, field);
629 clear: function(field){
630 field.el.removeClass(field.invalidClass);
632 Ext.form.Field.msgFx[field.msgFx].hide(field.errorEl, field);
634 field.el.dom.title = '';
639 mark: function(field, msg){
640 field.el.addClass(field.invalidClass);
641 if(!field.errorIcon){
642 var elp = field.getErrorCt();
643 if(!elp){ // field has no container el
644 field.el.dom.title = msg;
647 field.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
648 field.on('resize', field.alignErrorIcon, field);
649 field.on('destroy', function(){
650 Ext.destroy(this.errorIcon);
653 field.alignErrorIcon();
654 field.errorIcon.dom.qtip = msg;
655 field.errorIcon.dom.qclass = 'x-form-invalid-tip';
656 field.errorIcon.show();
658 clear: function(field){
659 field.el.removeClass(field.invalidClass);
661 field.errorIcon.dom.qtip = '';
662 field.errorIcon.hide();
664 field.el.dom.title = '';
670 // anything other than normal should be considered experimental
671 Ext.form.Field.msgFx = {
673 show: function(msgEl, f){
674 msgEl.setDisplayed('block');
677 hide : function(msgEl, f){
678 msgEl.setDisplayed(false).update('');
683 show: function(msgEl, f){
684 msgEl.slideIn('t', {stopFx:true});
687 hide : function(msgEl, f){
688 msgEl.slideOut('t', {stopFx:true,useDisplay:true});
693 show: function(msgEl, f){
695 msgEl.alignTo(f.el, 'tl-tr');
696 msgEl.slideIn('l', {stopFx:true});
699 hide : function(msgEl, f){
700 msgEl.slideOut('l', {stopFx:true,useDisplay:true});
704 Ext.reg('field', Ext.form.Field);