4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-form-field-Field'>/**
19 </span> * @class Ext.form.field.Field
21 This mixin provides a common interface for the logical behavior and state of form fields, including:
23 - Getter and setter methods for field values
24 - Events and methods for tracking value and validity changes
25 - Methods for triggering validation
27 **NOTE**: When implementing custom fields, it is most likely that you will want to extend the {@link Ext.form.field.Base}
28 component class rather than using this mixin directly, as BaseField contains additional logic for generating an
29 actual DOM complete with {@link Ext.form.Labelable label and error message} display and a form input field,
30 plus methods that bind the Field value getters and setters to the input field's value.
32 If you do want to implement this mixin directly and don't want to extend {@link Ext.form.field.Base}, then
33 you will most likely want to override the following methods with custom implementations: {@link #getValue},
34 {@link #setValue}, and {@link #getErrors}. Other methods may be overridden as needed but their base
35 implementations should be sufficient for common cases. You will also need to make sure that {@link #initField}
36 is called during the component's initialization.
39 * @docauthor Jason Johnston <jason@sencha.com>
41 Ext.define('Ext.form.field.Field', {
43 <span id='Ext-form-field-Field-property-isFormField'> /**
44 </span> * @property isFormField
46 * Flag denoting that this component is a Field. Always true.
50 <span id='Ext-form-field-Field-cfg-value'> /**
51 </span> * @cfg {Mixed} value A value to initialize this field with (defaults to undefined).
54 <span id='Ext-form-field-Field-cfg-name'> /**
55 </span> * @cfg {String} name The name of the field (defaults to undefined). By default this is used as the parameter
56 * name when including the {@link #getSubmitData field value} in a {@link Ext.form.Basic#submit form submit()}.
57 * To prevent the field from being included in the form submit, set {@link #submitValue} to <tt>false</tt>.
60 <span id='Ext-form-field-Field-cfg-disabled'> /**
61 </span> * @cfg {Boolean} disabled True to disable the field (defaults to false). Disabled Fields will not be
62 * {@link Ext.form.Basic#submit submitted}.</p>
66 <span id='Ext-form-field-Field-cfg-submitValue'> /**
67 </span> * @cfg {Boolean} submitValue Setting this to <tt>false</tt> will prevent the field from being
68 * {@link Ext.form.Basic#submit submitted} even when it is not disabled. Defaults to <tt>true</tt>.
72 <span id='Ext-form-field-Field-cfg-validateOnChange'> /**
73 </span> * @cfg {Boolean} validateOnChange
74 * <p>Specifies whether this field should be validated immediately whenever a change in its value is detected.
75 * Defaults to <tt>true</tt>. If the validation results in a change in the field's validity, a
76 * {@link #validitychange} event will be fired. This allows the field to show feedback about the
77 * validity of its contents immediately as the user is typing.</p>
78 * <p>When set to <tt>false</tt>, feedback will not be immediate. However the form will still be validated
79 * before submitting if the <tt>clientValidation</tt> option to {@link Ext.form.Basic#doAction} is
80 * enabled, or if the field or form are validated manually.</p>
81 * <p>See also {@link Ext.form.field.Base#checkChangeEvents}for controlling how changes to the field's value are detected.</p>
83 validateOnChange: true,
85 <span id='Ext-form-field-Field-property-suspendCheckChange'> /**
88 suspendCheckChange: 0,
90 <span id='Ext-form-field-Field-method-initField'> /**
91 </span> * Initializes this Field mixin on the current instance. Components using this mixin should call
92 * this method during their own initialization process.
94 initField: function() {
96 <span id='Ext-form-field-Field-event-change'> /**
97 </span> * @event change
98 * Fires when a user-initiated change is detected in the value of the field.
99 * @param {Ext.form.field.Field} this
100 * @param {Mixed} newValue The new value
101 * @param {Mixed} oldValue The original value
104 <span id='Ext-form-field-Field-event-validitychange'> /**
105 </span> * @event validitychange
106 * Fires when a change in the field's validity is detected.
107 * @param {Ext.form.field.Field} this
108 * @param {Boolean} isValid Whether or not the field is now valid
111 <span id='Ext-form-field-Field-event-dirtychange'> /**
112 </span> * @event dirtychange
113 * Fires when a change in the field's {@link #isDirty} state is detected.
114 * @param {Ext.form.field.Field} this
115 * @param {Boolean} isDirty Whether or not the field is now dirty
123 <span id='Ext-form-field-Field-method-initValue'> /**
125 * Initializes the field's value based on the initial config.
127 initValue: function() {
130 <span id='Ext-form-field-Field-property-originalValue'> /**
131 </span> * @property originalValue
133 * The original value of the field as configured in the {@link #value} configuration, or as loaded by
134 * the last form load operation if the form's {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}
135 * setting is <code>true</code>.
137 me.originalValue = me.lastValue = me.value;
139 // Set the initial value - prevent validation on initial set
140 me.suspendCheckChange++;
141 me.setValue(me.value);
142 me.suspendCheckChange--;
145 <span id='Ext-form-field-Field-method-getName'> /**
146 </span> * Returns the {@link Ext.form.field.Field#name name} attribute of the field. This is used as the parameter
147 * name when including the field value in a {@link Ext.form.Basic#submit form submit()}.
148 * @return {String} name The field {@link Ext.form.field.Field#name name}
150 getName: function() {
154 <span id='Ext-form-field-Field-method-getValue'> /**
155 </span> * Returns the current data value of the field. The type of value returned is particular to the type of the
156 * particular field (e.g. a Date object for {@link Ext.form.field.Date}).
157 * @return {Mixed} value The field value
159 getValue: function() {
163 <span id='Ext-form-field-Field-method-setValue'> /**
164 </span> * Sets a data value into the field and runs the change detection and validation.
165 * @param {Mixed} value The value to set
166 * @return {Ext.form.field.Field} this
168 setValue: function(value) {
175 <span id='Ext-form-field-Field-method-isEqual'> /**
176 </span> * Returns whether two field {@link #getValue values} are logically equal. Field implementations may override
177 * this to provide custom comparison logic appropriate for the particular field's data type.
178 * @param {Mixed} value1 The first value to compare
179 * @param {Mixed} value2 The second value to compare
180 * @return {Boolean} True if the values are equal, false if inequal.
182 isEqual: function(value1, value2) {
183 return String(value1) === String(value2);
186 <span id='Ext-form-field-Field-method-getSubmitData'> /**
187 </span> * <p>Returns the parameter(s) that would be included in a standard form submit for this field. Typically this
188 * will be an object with a single name-value pair, the name being this field's {@link #getName name} and the
189 * value being its current stringified value. More advanced field implementations may return more than one
190 * name-value pair.</p>
191 * <p>Note that the values returned from this method are not guaranteed to have been successfully
192 * {@link #validate validated}.</p>
193 * @return {Object} A mapping of submit parameter names to values; each value should be a string, or an array
194 * of strings if that particular name has multiple values. It can also return <tt>null</tt> if there are no
195 * parameters to be submitted.
197 getSubmitData: function() {
200 if (!me.disabled && me.submitValue && !me.isFileUpload()) {
202 data[me.getName()] = '' + me.getValue();
207 <span id='Ext-form-field-Field-method-getModelData'> /**
208 </span> * <p>Returns the value(s) that should be saved to the {@link Ext.data.Model} instance for this field, when
209 * {@link Ext.form.Basic#updateRecord} is called. Typically this will be an object with a single name-value
210 * pair, the name being this field's {@link #getName name} and the value being its current data value. More
211 * advanced field implementations may return more than one name-value pair. The returned values will be
212 * saved to the corresponding field names in the Model.</p>
213 * <p>Note that the values returned from this method are not guaranteed to have been successfully
214 * {@link #validate validated}.</p>
215 * @return {Object} A mapping of submit parameter names to values; each value should be a string, or an array
216 * of strings if that particular name has multiple values. It can also return <tt>null</tt> if there are no
217 * parameters to be submitted.
219 getModelData: function() {
222 if (!me.disabled && !me.isFileUpload()) {
224 data[me.getName()] = me.getValue();
229 <span id='Ext-form-field-Field-method-reset'> /**
230 </span> * Resets the current field value to the originally loaded value and clears any validation messages.
231 * See {@link Ext.form.Basic}.{@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}
236 me.setValue(me.originalValue);
238 // delete here so we reset back to the original state
242 <span id='Ext-form-field-Field-method-resetOriginalValue'> /**
243 </span> * Resets the field's {@link #originalValue} property so it matches the current {@link #getValue value}.
244 * This is called by {@link Ext.form.Basic}.{@link Ext.form.Basic#setValues setValues} if the form's
245 * {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad} property is set to true.
247 resetOriginalValue: function() {
248 this.originalValue = this.getValue();
252 <span id='Ext-form-field-Field-method-checkChange'> /**
253 </span> * <p>Checks whether the value of the field has changed since the last time it was checked. If the value
254 * has changed, it:</p>
256 * <li>Fires the {@link #change change event},</li>
257 * <li>Performs validation if the {@link #validateOnChange} config is enabled, firing the
258 * {@link #validationchange validationchange event} if the validity has changed, and</li>
259 * <li>Checks the {@link #isDirty dirty state} of the field and fires the {@link #dirtychange dirtychange event}
260 * if it has changed.</li>
263 checkChange: function() {
264 if (!this.suspendCheckChange) {
266 newVal = me.getValue(),
267 oldVal = me.lastValue;
268 if (!me.isEqual(newVal, oldVal) && !me.isDestroyed) {
269 me.lastValue = newVal;
270 me.fireEvent('change', me, newVal, oldVal);
271 me.onChange(newVal, oldVal);
276 <span id='Ext-form-field-Field-method-onChange'> /**
278 * Called when the field's value changes. Performs validation if the {@link #validateOnChange}
279 * config is enabled, and invokes the dirty check.
281 onChange: function(newVal, oldVal) {
282 if (this.validateOnChange) {
288 <span id='Ext-form-field-Field-method-isDirty'> /**
289 </span> * <p>Returns true if the value of this Field has been changed from its {@link #originalValue}.
290 * Will always return false if the field is disabled.</p>
291 * <p>Note that if the owning {@link Ext.form.Basic form} was configured with
292 * {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}
293 * then the {@link #originalValue} is updated when the values are loaded by
294 * {@link Ext.form.Basic}.{@link Ext.form.Basic#setValues setValues}.</p>
295 * @return {Boolean} True if this field has been changed from its original value (and
296 * is not disabled), false otherwise.
298 isDirty : function() {
300 return !me.disabled && !me.isEqual(me.getValue(), me.originalValue);
303 <span id='Ext-form-field-Field-method-checkDirty'> /**
304 </span> * Checks the {@link #isDirty} state of the field and if it has changed since the last time
305 * it was checked, fires the {@link #dirtychange} event.
307 checkDirty: function() {
309 isDirty = me.isDirty();
310 if (isDirty !== me.wasDirty) {
311 me.fireEvent('dirtychange', me, isDirty);
312 me.onDirtyChange(isDirty);
313 me.wasDirty = isDirty;
317 <span id='Ext-form-field-Field-property-onDirtyChange'> /**
318 </span> * @private Called when the field's dirty state changes.
319 * @param {Boolean} isDirty
321 onDirtyChange: Ext.emptyFn,
323 <span id='Ext-form-field-Field-method-getErrors'> /**
324 </span> * <p>Runs this field's validators and returns an array of error messages for any validation failures.
325 * This is called internally during validation and would not usually need to be used manually.</p>
326 * <p>Each subclass should override or augment the return value to provide their own errors.</p>
327 * @param {Mixed} value The value to get errors for (defaults to the current field value)
328 * @return {Array} All error messages for this field; an empty Array if none.
330 getErrors: function(value) {
334 <span id='Ext-form-field-Field-method-isValid'> /**
335 </span> * <p>Returns whether or not the field value is currently valid by {@link #getErrors validating} the
336 * field's current value. The {@link #validitychange} event will not be fired; use {@link #validate}
337 * instead if you want the event to fire. <b>Note</b>: {@link #disabled} fields are always treated as valid.</p>
338 * <p>Implementations are encouraged to ensure that this method does not have side-effects such as
339 * triggering error message display.</p>
340 * @return {Boolean} True if the value is valid, else false
342 isValid : function() {
344 return me.disabled || Ext.isEmpty(me.getErrors());
347 <span id='Ext-form-field-Field-method-validate'> /**
348 </span> * <p>Returns whether or not the field value is currently valid by {@link #getErrors validating} the
349 * field's current value, and fires the {@link #validitychange} event if the field's validity has
350 * changed since the last validation. <b>Note</b>: {@link #disabled} fields are always treated as valid.</p>
351 * <p>Custom implementations of this method are allowed to have side-effects such as triggering error
352 * message display. To validate without side-effects, use {@link #isValid}.</p>
353 * @return {Boolean} True if the value is valid, else false
355 validate : function() {
357 isValid = me.isValid();
358 if (isValid !== me.wasValid) {
359 me.wasValid = isValid;
360 me.fireEvent('validitychange', me, isValid);
365 <span id='Ext-form-field-Field-method-batchChanges'> /**
366 </span> * A utility for grouping a set of modifications which may trigger value changes into a single
367 * transaction, to prevent excessive firing of {@link #change} events. This is useful for instance
368 * if the field has sub-fields which are being updated as a group; you don't want the container
369 * field to check its own changed state for each subfield change.
370 * @param fn A function containing the transaction code
372 batchChanges: function(fn) {
373 this.suspendCheckChange++;
375 this.suspendCheckChange--;
379 <span id='Ext-form-field-Field-method-isFileUpload'> /**
380 </span> * Returns whether this Field is a file upload field; if it returns true, forms will use
381 * special techniques for {@link Ext.form.Basic#submit submitting the form} via AJAX. See
382 * {@link Ext.form.Basic#hasUpload} for details. If this returns true, the {@link #extractFileInput}
383 * method must also be implemented to return the corresponding file input element.
386 isFileUpload: function() {
390 <span id='Ext-form-field-Field-method-extractFileInput'> /**
391 </span> * Only relevant if the instance's {@link #isFileUpload} method returns true. Returns a reference
392 * to the file input DOM element holding the user's selected file. The input will be appended into
393 * the submission form and will not be returned, so this method should also create a replacement.
394 * @return {HTMLInputElement}
396 extractFileInput: function() {
400 <span id='Ext-form-field-Field-method-markInvalid'> /**
401 </span> * <p>Associate one or more error messages with this field. Components using this mixin should implement
402 * this method to update the component's rendering to display the messages.</p>
403 * <p><b>Note</b>: this method does not cause the Field's {@link #validate} or {@link #isValid} methods to
404 * return <code>false</code> if the value does <i>pass</i> validation. So simply marking a Field as invalid
405 * will not prevent submission of forms submitted with the {@link Ext.form.action.Submit#clientValidation}
406 * option set.</p>
407 * @param {String/Array} errors The error message(s) for the field.
410 markInvalid: Ext.emptyFn,
412 <span id='Ext-form-field-Field-method-clearInvalid'> /**
413 </span> * <p>Clear any invalid styles/messages for this field. Components using this mixin should implement
414 * this method to update the components rendering to clear any existing messages.</p>
415 * <p><b>Note</b>: this method does not cause the Field's {@link #validate} or {@link #isValid} methods to
416 * return <code>true</code> if the value does not <i>pass</i> validation. So simply clearing a field's errors
417 * will not necessarily allow submission of forms submitted with the {@link Ext.form.action.Submit#clientValidation}
418 * option set.</p>
421 clearInvalid: Ext.emptyFn