3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.form.Field"></div>/**
16 * @class Ext.form.Field
17 * @extends Ext.BoxComponent
18 * Base class for form fields that provides default event handling, sizing, value handling and other functionality.
21 * @param {Object} config Configuration options
24 Ext.form.Field = Ext.extend(Ext.BoxComponent, {
25 <div id="prop-Ext.form.Field-label"></div>/**
26 * <p>The label Element associated with this Field. <b>Only available after this Field has been rendered by a
27 * {@link form Ext.layout.FormLayout} layout manager.</b></p>
31 <div id="cfg-Ext.form.Field-inputType"></div>/**
32 * @cfg {String} inputType The type attribute for input fields -- e.g. radio, text, password, file (defaults
33 * to 'text'). The types 'file' and 'password' must be used to render those field types currently -- there are
34 * no separate Ext components for those. Note that if you use <tt>inputType:'file'</tt>, {@link #emptyText}
35 * is not supported and should be avoided.
37 <div id="cfg-Ext.form.Field-tabIndex"></div>/**
38 * @cfg {Number} tabIndex The tabIndex for this field. Note this only applies to fields that are rendered,
39 * not those which are built via applyTo (defaults to undefined).
41 <div id="cfg-Ext.form.Field-value"></div>/**
42 * @cfg {Mixed} value A value to initialize this field with (defaults to undefined).
44 <div id="cfg-Ext.form.Field-name"></div>/**
45 * @cfg {String} name The field's HTML name attribute (defaults to '').
46 * <b>Note</b>: this property must be set if this field is to be automatically included with
47 * {@link Ext.form.BasicForm#submit form submit()}.
49 <div id="cfg-Ext.form.Field-cls"></div>/**
50 * @cfg {String} cls A custom CSS class to apply to the field's underlying element (defaults to '').
53 <div id="cfg-Ext.form.Field-invalidClass"></div>/**
54 * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to 'x-form-invalid')
56 invalidClass : 'x-form-invalid',
57 <div id="cfg-Ext.form.Field-invalidText"></div>/**
58 * @cfg {String} invalidText The error text to use when marking a field invalid and no message is provided
59 * (defaults to 'The value in this field is invalid')
61 invalidText : 'The value in this field is invalid',
62 <div id="cfg-Ext.form.Field-focusClass"></div>/**
63 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to 'x-form-focus')
65 focusClass : 'x-form-focus',
66 <div id="cfg-Ext.form.Field-preventMark"></div>/**
67 * @cfg {Boolean} preventMark
68 * <tt>true</tt> to disable {@link #markInvalid marking the field invalid}.
69 * Defaults to <tt>false</tt>.
71 <div id="cfg-Ext.form.Field-validationEvent"></div>/**
72 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
73 automatic validation (defaults to 'keyup').
75 validationEvent : 'keyup',
76 <div id="cfg-Ext.form.Field-validateOnBlur"></div>/**
77 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
79 validateOnBlur : true,
80 <div id="cfg-Ext.form.Field-validationDelay"></div>/**
81 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation
82 * is initiated (defaults to 250)
84 validationDelay : 250,
85 <div id="cfg-Ext.form.Field-autoCreate"></div>/**
86 * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or true for a default
87 * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.
88 * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details. Defaults to:</p>
89 * <pre><code>{tag: 'input', type: 'text', size: '20', autocomplete: 'off'}</code></pre>
91 defaultAutoCreate : {tag: 'input', type: 'text', size: '20', autocomplete: 'off'},
92 <div id="cfg-Ext.form.Field-fieldClass"></div>/**
93 * @cfg {String} fieldClass The default CSS class for the field (defaults to 'x-form-field')
95 fieldClass : 'x-form-field',
96 <div id="cfg-Ext.form.Field-msgTarget"></div>/**
97 * @cfg {String} msgTarget <p>The location where the message text set through {@link #markInvalid} should display.
98 * Must be one of the following values:</p>
99 * <div class="mdetail-params"><ul>
100 * <li><code>qtip</code> Display a quick tip containing the message when the user hovers over the field. This is the default.
101 * <div class="subdesc"><b>{@link Ext.QuickTips#init Ext.QuickTips.init} must have been called for this setting to work.</b></div</li>
102 * <li><code>title</code> Display the message in a default browser title attribute popup.</li>
103 * <li><code>under</code> Add a block div beneath the field containing the error message.</li>
104 * <li><code>side</code> Add an error icon to the right of the field, displaying the message in a popup on hover.</li>
105 * <li><code>[element id]</code> Add the error message directly to the innerHTML of the specified element.</li>
109 <div id="cfg-Ext.form.Field-msgFx"></div>/**
110 * @cfg {String} msgFx <b>Experimental</b> The effect used when displaying a validation message under the field
111 * (defaults to 'normal').
114 <div id="cfg-Ext.form.Field-readOnly"></div>/**
115 * @cfg {Boolean} readOnly <tt>true</tt> to mark the field as readOnly in HTML
116 * (defaults to <tt>false</tt>).
117 * <br><p><b>Note</b>: this only sets the element's readOnly DOM attribute.
118 * Setting <code>readOnly=true</code>, for example, will not disable triggering a
119 * ComboBox or DateField; it gives you the option of forcing the user to choose
120 * via the trigger without typing in the text box. To hide the trigger use
121 * <code>{@link Ext.form.TriggerField#hideTrigger hideTrigger}</code>.</p>
124 <div id="cfg-Ext.form.Field-disabled"></div>/**
125 * @cfg {Boolean} disabled True to disable the field (defaults to false).
126 * <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>,
127 * disabled Fields will not be {@link Ext.form.BasicForm#submit submitted}.</p>
130 <div id="cfg-Ext.form.Field-submitValue"></div>/**
131 * @cfg {Boolean} submitValue False to clear the name attribute on the field so that it is not submitted during a form post.
132 * Defaults to <tt>true</tt>.
146 initComponent : function(){
147 Ext.form.Field.superclass.initComponent.call(this);
149 <div id="event-Ext.form.Field-focus"></div>/**
151 * Fires when this field receives input focus.
152 * @param {Ext.form.Field} this
155 <div id="event-Ext.form.Field-blur"></div>/**
157 * Fires when this field loses input focus.
158 * @param {Ext.form.Field} this
161 <div id="event-Ext.form.Field-specialkey"></div>/**
163 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.
164 * To handle other keys see {@link Ext.Panel#keys} or {@link Ext.KeyMap}.
165 * You can check {@link Ext.EventObject#getKey} to determine which key was pressed.
166 * For example: <pre><code>
167 var form = new Ext.form.FormPanel({
170 fieldLabel: 'Field 1',
174 fieldLabel: 'Field 2',
177 specialkey: function(field, e){
178 // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
179 // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
180 if (e.{@link Ext.EventObject#getKey getKey()} == e.ENTER) {
181 var form = field.ownerCt.getForm();
191 * @param {Ext.form.Field} this
192 * @param {Ext.EventObject} e The event object
195 <div id="event-Ext.form.Field-change"></div>/**
197 * Fires just before the field blurs if the field value has changed.
198 * @param {Ext.form.Field} this
199 * @param {Mixed} newValue The new value
200 * @param {Mixed} oldValue The original value
203 <div id="event-Ext.form.Field-invalid"></div>/**
205 * Fires after the field has been marked as invalid.
206 * @param {Ext.form.Field} this
207 * @param {String} msg The validation message
210 <div id="event-Ext.form.Field-valid"></div>/**
212 * Fires after the field has been validated with no errors.
213 * @param {Ext.form.Field} this
219 <div id="method-Ext.form.Field-getName"></div>/**
220 * Returns the {@link Ext.form.Field#name name} or {@link Ext.form.ComboBox#hiddenName hiddenName}
221 * attribute of the field if available.
222 * @return {String} name The field {@link Ext.form.Field#name name} or {@link Ext.form.ComboBox#hiddenName hiddenName}
224 getName : function(){
225 return this.rendered && this.el.dom.name ? this.el.dom.name : this.name || this.id || '';
229 onRender : function(ct, position){
231 var cfg = this.getAutoCreate();
234 cfg.name = this.name || this.id;
237 cfg.type = this.inputType;
241 Ext.form.Field.superclass.onRender.call(this, ct, position);
242 if(this.submitValue === false){
243 this.el.dom.removeAttribute('name');
245 var type = this.el.dom.type;
247 if(type == 'password'){
250 this.el.addClass('x-form-'+type);
253 this.setReadOnly(true);
255 if(this.tabIndex !== undefined){
256 this.el.dom.setAttribute('tabIndex', this.tabIndex);
259 this.el.addClass([this.fieldClass, this.cls]);
263 getItemCt : function(){
268 initValue : function(){
269 if(this.value !== undefined){
270 this.setValue(this.value);
271 }else if(!Ext.isEmpty(this.el.dom.value) && this.el.dom.value != this.emptyText){
272 this.setValue(this.el.dom.value);
274 <div id="prop-Ext.form.Field-originalValue"></div>/**
275 * The original value of the field as configured in the {@link #value} configuration, or
276 * as loaded by the last form load operation if the form's {@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
277 * setting is <code>true</code>.
279 * @property originalValue
281 this.originalValue = this.getValue();
284 <div id="method-Ext.form.Field-isDirty"></div>/**
285 * <p>Returns true if the value of this Field has been changed from its original value.
286 * Will return false if the field is disabled or has not been rendered yet.</p>
287 * <p>Note that if the owning {@link Ext.form.BasicForm form} was configured with
288 * {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
289 * then the <i>original value</i> is updated when the values are loaded by
290 * {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#setValues setValues}.</p>
291 * @return {Boolean} True if this field has been changed from its original value (and
292 * is not disabled), false otherwise.
294 isDirty : function() {
295 if(this.disabled || !this.rendered) {
298 return String(this.getValue()) !== String(this.originalValue);
301 <div id="method-Ext.form.Field-setReadOnly"></div>/**
302 * Sets the read only state of this field.
303 * @param {Boolean} readOnly Whether the field should be read only.
305 setReadOnly : function(readOnly){
307 this.el.dom.readOnly = readOnly;
309 this.readOnly = readOnly;
313 afterRender : function(){
314 Ext.form.Field.superclass.afterRender.call(this);
320 fireKey : function(e){
321 if(e.isSpecialKey()){
322 this.fireEvent('specialkey', this, e);
326 <div id="method-Ext.form.Field-reset"></div>/**
327 * Resets the current field value to the originally loaded value and clears any validation messages.
328 * See {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
331 this.setValue(this.originalValue);
336 initEvents : function(){
337 this.mon(this.el, Ext.EventManager.getKeyEvent(), this.fireKey, this);
338 this.mon(this.el, 'focus', this.onFocus, this);
340 // standardise buffer across all browsers + OS-es for consistent event order.
341 // (the 10ms buffer for Editors fixes a weird FF/Win editor issue when changing OS window focus)
342 this.mon(this.el, 'blur', this.onBlur, this, this.inEditor ? {buffer:10} : null);
346 preFocus: Ext.emptyFn,
349 onFocus : function(){
352 this.el.addClass(this.focusClass);
355 this.hasFocus = true;
356 <div id="prop-Ext.form.Field-startValue"></div>/**
357 * <p>The value that the Field had at the time it was last focused. This is the value that is passed
358 * to the {@link #change} event which is fired if the value has been changed when the Field is blurred.</p>
359 * <p><b>This will be undefined until the Field has been visited.</b> Compare {@link #originalValue}.</p>
361 * @property startValue
363 this.startValue = this.getValue();
364 this.fireEvent('focus', this);
369 beforeBlur : Ext.emptyFn,
375 this.el.removeClass(this.focusClass);
377 this.hasFocus = false;
378 if(this.validationEvent !== false && (this.validateOnBlur || this.validationEvent == 'blur')){
381 var v = this.getValue();
382 if(String(v) !== String(this.startValue)){
383 this.fireEvent('change', this, v, this.startValue);
385 this.fireEvent('blur', this);
390 postBlur : Ext.emptyFn,
392 <div id="method-Ext.form.Field-isValid"></div>/**
393 * Returns whether or not the field value is currently valid by
394 * {@link #validateValue validating} the {@link #processValue processed value}
395 * of the field. <b>Note</b>: {@link #disabled} fields are ignored.
396 * @param {Boolean} preventMark True to disable marking the field invalid
397 * @return {Boolean} True if the value is valid, else false
399 isValid : function(preventMark){
403 var restore = this.preventMark;
404 this.preventMark = preventMark === true;
405 var v = this.validateValue(this.processValue(this.getRawValue()));
406 this.preventMark = restore;
410 <div id="method-Ext.form.Field-validate"></div>/**
411 * Validates the field value
412 * @return {Boolean} True if the value is valid, else false
414 validate : function(){
415 if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
422 <div id="method-Ext.form.Field-processValue"></div>/**
423 * This method should only be overridden if necessary to prepare raw values
424 * for validation (see {@link #validate} and {@link #isValid}). This method
425 * is expected to return the processed value for the field which will
426 * be used for validation (see validateValue method).
427 * @param {Mixed} value
429 processValue : function(value){
433 <div id="method-Ext.form.Field-validateValue"></div>/**
434 * Uses getErrors to build an array of validation errors. If any errors are found, markInvalid is called
435 * with the first and false is returned, otherwise true is returned. Previously, subclasses were invited
436 * to provide an implementation of this to process validations - from 3.2 onwards getErrors should be
437 * overridden instead.
438 * @param {Mixed} The current value of the field
439 * @return {Boolean} True if all validations passed, false if one or more failed
441 validateValue : function(value) {
442 //currently, we only show 1 error at a time for a field, so just use the first one
443 var error = this.getErrors(value)[0];
445 if (error == undefined) {
448 this.markInvalid(error);
453 <div id="method-Ext.form.Field-getErrors"></div>/**
454 * Runs this field's validators and returns an array of error messages for any validation failures.
455 * This is called internally during validation and would not usually need to be used manually.
456 * Each subclass should override or augment the return value to provide their own errors
457 * @return {Array} All error messages for this field
459 getErrors: function() {
463 <div id="method-Ext.form.Field-getActiveError"></div>/**
464 * Gets the active error message for this field.
465 * @return {String} Returns the active error message on the field, if there is no error, an empty string is returned.
467 getActiveError : function(){
468 return this.activeError || '';
471 <div id="method-Ext.form.Field-markInvalid"></div>/**
472 * <p>Display an error message associated with this field, using {@link #msgTarget} to determine how to
473 * display the message and applying {@link #invalidClass} to the field's UI element.</p>
474 * <p><b>Note</b>: this method does not cause the Field's {@link #validate} method to return <code>false</code>
475 * if the value does <i>pass</i> validation. So simply marking a Field as invalid will not prevent
476 * submission of forms submitted with the {@link Ext.form.Action.Submit#clientValidation} option set.</p>
477 * {@link #isValid invalid}.
478 * @param {String} msg (optional) The validation message (defaults to {@link #invalidText})
480 markInvalid : function(msg){
481 //don't set the error icon if we're not rendered or marking is prevented
482 if (this.rendered && !this.preventMark) {
483 msg = msg || this.invalidText;
485 var mt = this.getMessageHandler();
488 }else if(this.msgTarget){
489 this.el.addClass(this.invalidClass);
490 var t = Ext.getDom(this.msgTarget);
493 t.style.display = this.msgDisplay;
498 this.setActiveError(msg);
501 <div id="method-Ext.form.Field-clearInvalid"></div>/**
502 * Clear any invalid styles/messages for this field
504 clearInvalid : function(){
505 //don't remove the error icon if we're not rendered or marking is prevented
506 if (this.rendered && !this.preventMark) {
507 this.el.removeClass(this.invalidClass);
508 var mt = this.getMessageHandler();
511 }else if(this.msgTarget){
512 this.el.removeClass(this.invalidClass);
513 var t = Ext.getDom(this.msgTarget);
516 t.style.display = 'none';
521 this.unsetActiveError();
524 <div id="method-Ext.form.Field-setActiveError"></div>/**
525 * Sets the current activeError to the given string. Fires the 'invalid' event.
526 * This does not set up the error icon, only sets the message and fires the event. To show the error icon,
527 * use markInvalid instead, which calls this method internally
528 * @param {String} msg The error message
529 * @param {Boolean} suppressEvent True to suppress the 'invalid' event from being fired
531 setActiveError: function(msg, suppressEvent) {
532 this.activeError = msg;
533 if (suppressEvent !== true) this.fireEvent('invalid', this, msg);
536 <div id="method-Ext.form.Field-unsetActiveError"></div>/**
537 * Clears the activeError and fires the 'valid' event. This is called internally by clearInvalid and would not
538 * usually need to be called manually
539 * @param {Boolean} suppressEvent True to suppress the 'invalid' event from being fired
541 unsetActiveError: function(suppressEvent) {
542 delete this.activeError;
543 if (suppressEvent !== true) this.fireEvent('valid', this);
547 getMessageHandler : function(){
548 return Ext.form.MessageTargets[this.msgTarget];
552 getErrorCt : function(){
553 return this.el.findParent('.x-form-element', 5, true) || // use form element wrap if available
554 this.el.findParent('.x-form-field-wrap', 5, true); // else direct field wrap
557 // Alignment for 'under' target
558 alignErrorEl : function(){
559 this.errorEl.setWidth(this.getErrorCt().getWidth(true) - 20);
562 // Alignment for 'side' target
563 alignErrorIcon : function(){
564 this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
567 <div id="method-Ext.form.Field-getRawValue"></div>/**
568 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
569 * @return {Mixed} value The field value
571 getRawValue : function(){
572 var v = this.rendered ? this.el.getValue() : Ext.value(this.value, '');
573 if(v === this.emptyText){
579 <div id="method-Ext.form.Field-getValue"></div>/**
580 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
581 * @return {Mixed} value The field value
583 getValue : function(){
587 var v = this.el.getValue();
588 if(v === this.emptyText || v === undefined){
594 <div id="method-Ext.form.Field-setRawValue"></div>/**
595 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
596 * @param {Mixed} value The value to set
597 * @return {Mixed} value The field value that is set
599 setRawValue : function(v){
600 return this.rendered ? (this.el.dom.value = (Ext.isEmpty(v) ? '' : v)) : '';
603 <div id="method-Ext.form.Field-setValue"></div>/**
604 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
605 * @param {Mixed} value The value to set
606 * @return {Ext.form.Field} this
608 setValue : function(v){
611 this.el.dom.value = (Ext.isEmpty(v) ? '' : v);
617 // private, does not work for all fields
618 append : function(v){
619 this.setValue([this.getValue(), v].join(''));
622 <div id="cfg-Ext.form.Field-autoWidth"></div>/**
623 * @cfg {Boolean} autoWidth @hide
625 <div id="cfg-Ext.form.Field-autoHeight"></div>/**
626 * @cfg {Boolean} autoHeight @hide
629 <div id="cfg-Ext.form.Field-autoEl"></div>/**
630 * @cfg {String} autoEl @hide
635 Ext.form.MessageTargets = {
637 mark: function(field, msg){
638 field.el.addClass(field.invalidClass);
639 field.el.dom.qtip = msg;
640 field.el.dom.qclass = 'x-form-invalid-tip';
641 if(Ext.QuickTips){ // fix for floating editors interacting with DND
642 Ext.QuickTips.enable();
645 clear: function(field){
646 field.el.removeClass(field.invalidClass);
647 field.el.dom.qtip = '';
651 mark: function(field, msg){
652 field.el.addClass(field.invalidClass);
653 field.el.dom.title = msg;
655 clear: function(field){
656 field.el.dom.title = '';
660 mark: function(field, msg){
661 field.el.addClass(field.invalidClass);
663 var elp = field.getErrorCt();
664 if(!elp){ // field has no container el
665 field.el.dom.title = msg;
668 field.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
669 field.on('resize', field.alignErrorEl, field);
670 field.on('destroy', function(){
671 Ext.destroy(this.errorEl);
674 field.alignErrorEl();
675 field.errorEl.update(msg);
676 Ext.form.Field.msgFx[field.msgFx].show(field.errorEl, field);
678 clear: function(field){
679 field.el.removeClass(field.invalidClass);
681 Ext.form.Field.msgFx[field.msgFx].hide(field.errorEl, field);
683 field.el.dom.title = '';
688 mark: function(field, msg){
689 field.el.addClass(field.invalidClass);
690 if(!field.errorIcon){
691 var elp = field.getErrorCt();
692 // field has no container el
694 field.el.dom.title = msg;
697 field.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
699 field.ownerCt.on('afterlayout', field.alignErrorIcon, field);
700 field.ownerCt.on('expand', field.alignErrorIcon, field);
702 field.on('resize', field.alignErrorIcon, field);
703 field.on('destroy', function(){
704 Ext.destroy(this.errorIcon);
707 field.alignErrorIcon();
708 field.errorIcon.dom.qtip = msg;
709 field.errorIcon.dom.qclass = 'x-form-invalid-tip';
710 field.errorIcon.show();
712 clear: function(field){
713 field.el.removeClass(field.invalidClass);
715 field.errorIcon.dom.qtip = '';
716 field.errorIcon.hide();
718 field.el.dom.title = '';
724 // anything other than normal should be considered experimental
725 Ext.form.Field.msgFx = {
727 show: function(msgEl, f){
728 msgEl.setDisplayed('block');
731 hide : function(msgEl, f){
732 msgEl.setDisplayed(false).update('');
737 show: function(msgEl, f){
738 msgEl.slideIn('t', {stopFx:true});
741 hide : function(msgEl, f){
742 msgEl.slideOut('t', {stopFx:true,useDisplay:true});
747 show: function(msgEl, f){
749 msgEl.alignTo(f.el, 'tl-tr');
750 msgEl.slideIn('l', {stopFx:true});
753 hide : function(msgEl, f){
754 msgEl.slideOut('l', {stopFx:true,useDisplay:true});
758 Ext.reg('field', Ext.form.Field);