3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.form.Field"></div>/**
9 * @class Ext.form.Field
10 * @extends Ext.BoxComponent
11 * Base class for form fields that provides default event handling, sizing, value handling and other functionality.
14 * @param {Object} config Configuration options
17 Ext.form.Field = Ext.extend(Ext.BoxComponent, {
18 <div id="cfg-Ext.form.Field-inputType"></div>/**
19 * @cfg {String} inputType The type attribute for input fields -- e.g. radio, text, password, file (defaults
20 * to "text"). The types "file" and "password" must be used to render those field types currently -- there are
21 * no separate Ext components for those. Note that if you use <tt>inputType:'file'</tt>, {@link #emptyText}
22 * is not supported and should be avoided.
24 <div id="cfg-Ext.form.Field-tabIndex"></div>/**
25 * @cfg {Number} tabIndex The tabIndex for this field. Note this only applies to fields that are rendered,
26 * not those which are built via applyTo (defaults to undefined).
28 <div id="cfg-Ext.form.Field-value"></div>/**
29 * @cfg {Mixed} value A value to initialize this field with (defaults to undefined).
31 <div id="cfg-Ext.form.Field-name"></div>/**
32 * @cfg {String} name The field's HTML name attribute (defaults to "").
33 * <b>Note</b>: this property must be set if this field is to be automatically included with
34 * {@link Ext.form.BasicForm#submit form submit()}.
36 <div id="cfg-Ext.form.Field-cls"></div>/**
37 * @cfg {String} cls A custom CSS class to apply to the field's underlying element (defaults to "").
40 <div id="cfg-Ext.form.Field-invalidClass"></div>/**
41 * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
43 invalidClass : "x-form-invalid",
44 <div id="cfg-Ext.form.Field-invalidText"></div>/**
45 * @cfg {String} invalidText The error text to use when marking a field invalid and no message is provided
46 * (defaults to "The value in this field is invalid")
48 invalidText : "The value in this field is invalid",
49 <div id="cfg-Ext.form.Field-focusClass"></div>/**
50 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
52 focusClass : "x-form-focus",
53 <div id="cfg-Ext.form.Field-validationEvent"></div>/**
54 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
55 automatic validation (defaults to "keyup").
57 validationEvent : "keyup",
58 <div id="cfg-Ext.form.Field-validateOnBlur"></div>/**
59 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
61 validateOnBlur : true,
62 <div id="cfg-Ext.form.Field-validationDelay"></div>/**
63 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation
64 * is initiated (defaults to 250)
66 validationDelay : 250,
67 <div id="cfg-Ext.form.Field-autoCreate"></div>/**
68 * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or true for a default
69 * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.
70 * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details. Defaults to:</p>
71 * <pre><code>{tag: "input", type: "text", size: "20", autocomplete: "off"}</code></pre>
73 defaultAutoCreate : {tag: "input", type: "text", size: "20", autocomplete: "off"},
74 <div id="cfg-Ext.form.Field-fieldClass"></div>/**
75 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field")
77 fieldClass : "x-form-field",
78 <div id="cfg-Ext.form.Field-msgTarget"></div>/**
79 * @cfg {String} msgTarget The location where error text should display. Should be one of the following values
80 * (defaults to 'qtip'):
83 ----------- ----------------------------------------------------------------------
84 qtip Display a quick tip when the user hovers over the field
85 title Display a default browser title attribute popup
86 under Add a block div beneath the field containing the error text
87 side Add an error icon to the right of the field with a popup on hover
88 [element id] Add the error text directly to the innerHTML of the specified element
92 <div id="cfg-Ext.form.Field-msgFx"></div>/**
93 * @cfg {String} msgFx <b>Experimental</b> The effect used when displaying a validation message under the field
94 * (defaults to 'normal').
97 <div id="cfg-Ext.form.Field-readOnly"></div>/**
98 * @cfg {Boolean} readOnly <tt>true</tt> to mark the field as readOnly in HTML
99 * (defaults to <tt>false</tt>).
100 * <br><p><b>Note</b>: this only sets the element's readOnly DOM attribute.
101 * Setting <code>readOnly=true</code>, for example, will not disable triggering a
102 * ComboBox or DateField; it gives you the option of forcing the user to choose
103 * via the trigger without typing in the text box. To hide the trigger use
104 * <code>{@link Ext.form.TriggerField#hideTrigger hideTrigger}</code>.</p>
107 <div id="cfg-Ext.form.Field-disabled"></div>/**
108 * @cfg {Boolean} disabled True to disable the field (defaults to false).
109 * <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>,
110 * disabled Fields will not be {@link Ext.form.BasicForm#submit submitted}.</p>
121 initComponent : function(){
122 Ext.form.Field.superclass.initComponent.call(this);
124 <div id="event-Ext.form.Field-focus"></div>/**
126 * Fires when this field receives input focus.
127 * @param {Ext.form.Field} this
130 <div id="event-Ext.form.Field-blur"></div>/**
132 * Fires when this field loses input focus.
133 * @param {Ext.form.Field} this
136 <div id="event-Ext.form.Field-specialkey"></div>/**
138 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.
139 * To handle other keys see {@link Ext.Panel#keys} or {@link Ext.KeyMap}.
140 * You can check {@link Ext.EventObject#getKey} to determine which key was pressed.
141 * For example: <pre><code>
142 var form = new Ext.form.FormPanel({
145 fieldLabel: 'Field 1',
149 fieldLabel: 'Field 2',
152 specialkey: function(field, e){
153 // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
154 // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
155 if (e.{@link Ext.EventObject#getKey getKey()} == e.ENTER) {
156 var form = field.ownerCt.getForm();
166 * @param {Ext.form.Field} this
167 * @param {Ext.EventObject} e The event object
170 <div id="event-Ext.form.Field-change"></div>/**
172 * Fires just before the field blurs if the field value has changed.
173 * @param {Ext.form.Field} this
174 * @param {Mixed} newValue The new value
175 * @param {Mixed} oldValue The original value
178 <div id="event-Ext.form.Field-invalid"></div>/**
180 * Fires after the field has been marked as invalid.
181 * @param {Ext.form.Field} this
182 * @param {String} msg The validation message
185 <div id="event-Ext.form.Field-valid"></div>/**
187 * Fires after the field has been validated with no errors.
188 * @param {Ext.form.Field} this
194 <div id="method-Ext.form.Field-getName"></div>/**
195 * Returns the {@link Ext.form.Field#name name} or {@link Ext.form.ComboBox#hiddenName hiddenName}
196 * attribute of the field if available.
197 * @return {String} name The field {@link Ext.form.Field#name name} or {@link Ext.form.ComboBox#hiddenName hiddenName}
200 return this.rendered && this.el.dom.name ? this.el.dom.name : this.name || this.id || '';
204 onRender : function(ct, position){
206 var cfg = this.getAutoCreate();
209 cfg.name = this.name || this.id;
212 cfg.type = this.inputType;
216 Ext.form.Field.superclass.onRender.call(this, ct, position);
218 var type = this.el.dom.type;
220 if(type == 'password'){
223 this.el.addClass('x-form-'+type);
226 this.el.dom.readOnly = true;
228 if(this.tabIndex !== undefined){
229 this.el.dom.setAttribute('tabIndex', this.tabIndex);
232 this.el.addClass([this.fieldClass, this.cls]);
236 getItemCt : function(){
237 return this.el.up('.x-form-item', 4);
241 initValue : function(){
242 if(this.value !== undefined){
243 this.setValue(this.value);
244 }else if(!Ext.isEmpty(this.el.dom.value) && this.el.dom.value != this.emptyText){
245 this.setValue(this.el.dom.value);
247 <div id="prop-Ext.form.Field-originalValue"></div>/**
248 * The original value of the field as configured in the {@link #value} configuration, or
249 * as loaded by the last form load operation if the form's {@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
250 * setting is <code>true</code>.
252 * @property originalValue
254 this.originalValue = this.getValue();
257 <div id="method-Ext.form.Field-isDirty"></div>/**
258 * <p>Returns true if the value of this Field has been changed from its original value.
259 * Will return false if the field is disabled or has not been rendered yet.</p>
260 * <p>Note that if the owning {@link Ext.form.BasicForm form} was configured with
261 * {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
262 * then the <i>original value</i> is updated when the values are loaded by
263 * {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#setValues setValues}.</p>
264 * @return {Boolean} True if this field has been changed from its original value (and
265 * is not disabled), false otherwise.
267 isDirty : function() {
268 if(this.disabled || !this.rendered) {
271 return String(this.getValue()) !== String(this.originalValue);
275 afterRender : function(){
276 Ext.form.Field.superclass.afterRender.call(this);
282 fireKey : function(e){
283 if(e.isSpecialKey()){
284 this.fireEvent("specialkey", this, e);
288 <div id="method-Ext.form.Field-reset"></div>/**
289 * Resets the current field value to the originally loaded value and clears any validation messages.
290 * See {@link Ext.form.BasicForm}.{@link Ext.form.BasicForm#trackResetOnLoad trackResetOnLoad}
293 this.setValue(this.originalValue);
298 initEvents : function(){
299 this.mon(this.el, Ext.EventManager.useKeydown ? "keydown" : "keypress", this.fireKey, this);
300 this.mon(this.el, 'focus', this.onFocus, this);
302 // fix weird FF/Win editor issue when changing OS window focus
303 var o = this.inEditor && Ext.isWindows && Ext.isGecko ? {buffer:10} : null;
304 this.mon(this.el, 'blur', this.onBlur, this, o);
308 onFocus : function(){
310 this.el.addClass(this.focusClass);
313 this.hasFocus = true;
314 this.startValue = this.getValue();
315 this.fireEvent("focus", this);
320 beforeBlur : Ext.emptyFn,
326 this.el.removeClass(this.focusClass);
328 this.hasFocus = false;
329 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
332 var v = this.getValue();
333 if(String(v) !== String(this.startValue)){
334 this.fireEvent('change', this, v, this.startValue);
336 this.fireEvent("blur", this);
339 <div id="method-Ext.form.Field-isValid"></div>/**
340 * Returns whether or not the field value is currently valid
341 * @param {Boolean} preventMark True to disable marking the field invalid
342 * @return {Boolean} True if the value is valid, else false
344 isValid : function(preventMark){
348 var restore = this.preventMark;
349 this.preventMark = preventMark === true;
350 var v = this.validateValue(this.processValue(this.getRawValue()));
351 this.preventMark = restore;
355 <div id="method-Ext.form.Field-validate"></div>/**
356 * Validates the field value
357 * @return {Boolean} True if the value is valid, else false
359 validate : function(){
360 if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
367 // protected - should be overridden by subclasses if necessary to prepare raw values for validation
368 processValue : function(value){
373 // Subclasses should provide the validation implementation by overriding this
374 validateValue : function(value){
378 <div id="method-Ext.form.Field-markInvalid"></div>/**
379 * Mark this field as invalid, using {@link #msgTarget} to determine how to display the error and
380 * applying {@link #invalidClass} to the field's element.
381 * @param {String} msg (optional) The validation message (defaults to {@link #invalidText})
383 markInvalid : function(msg){
384 if(!this.rendered || this.preventMark){ // not rendered
387 msg = msg || this.invalidText;
389 var mt = this.getMessageHandler();
392 }else if(this.msgTarget){
393 this.el.addClass(this.invalidClass);
394 var t = Ext.getDom(this.msgTarget);
397 t.style.display = this.msgDisplay;
400 this.fireEvent('invalid', this, msg);
403 <div id="method-Ext.form.Field-clearInvalid"></div>/**
404 * Clear any invalid styles/messages for this field
406 clearInvalid : function(){
407 if(!this.rendered || this.preventMark){ // not rendered
410 this.el.removeClass(this.invalidClass);
411 var mt = this.getMessageHandler();
414 }else if(this.msgTarget){
415 this.el.removeClass(this.invalidClass);
416 var t = Ext.getDom(this.msgTarget);
419 t.style.display = 'none';
422 this.fireEvent('valid', this);
426 getMessageHandler : function(){
427 return Ext.form.MessageTargets[this.msgTarget];
431 getErrorCt : function(){
432 return this.el.findParent('.x-form-element', 5, true) || // use form element wrap if available
433 this.el.findParent('.x-form-field-wrap', 5, true); // else direct field wrap
437 alignErrorIcon : function(){
438 this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
441 <div id="method-Ext.form.Field-getRawValue"></div>/**
442 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
443 * @return {Mixed} value The field value
445 getRawValue : function(){
446 var v = this.rendered ? this.el.getValue() : Ext.value(this.value, '');
447 if(v === this.emptyText){
453 <div id="method-Ext.form.Field-getValue"></div>/**
454 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
455 * @return {Mixed} value The field value
457 getValue : function(){
461 var v = this.el.getValue();
462 if(v === this.emptyText || v === undefined){
468 <div id="method-Ext.form.Field-setRawValue"></div>/**
469 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
470 * @param {Mixed} value The value to set
471 * @return {Mixed} value The field value that is set
473 setRawValue : function(v){
474 return (this.el.dom.value = (Ext.isEmpty(v) ? '' : v));
477 <div id="method-Ext.form.Field-setValue"></div>/**
478 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
479 * @param {Mixed} value The value to set
480 * @return {Ext.form.Field} this
482 setValue : function(v){
485 this.el.dom.value = (Ext.isEmpty(v) ? '' : v);
491 // private, does not work for all fields
492 append : function(v){
493 this.setValue([this.getValue(), v].join(''));
497 adjustSize : function(w, h){
498 var s = Ext.form.Field.superclass.adjustSize.call(this, w, h);
499 s.width = this.adjustWidth(this.el.dom.tagName, s.width);
501 var ct = this.getItemCt();
502 s.width -= ct.getFrameWidth('lr');
503 s.height -= ct.getFrameWidth('tb');
509 adjustWidth : function(tag, w){
510 if(typeof w == 'number' && (Ext.isIE && (Ext.isIE6 || !Ext.isStrict)) && /input|textarea/i.test(tag) && !this.inEditor){
516 <div id="cfg-Ext.form.Field-autoWidth"></div>/**
517 * @cfg {Boolean} autoWidth @hide
519 <div id="cfg-Ext.form.Field-autoHeight"></div>/**
520 * @cfg {Boolean} autoHeight @hide
523 <div id="cfg-Ext.form.Field-autoEl"></div>/**
524 * @cfg {String} autoEl @hide
529 Ext.form.MessageTargets = {
531 mark: function(field, msg){
532 field.el.addClass(field.invalidClass);
533 field.el.dom.qtip = msg;
534 field.el.dom.qclass = 'x-form-invalid-tip';
535 if(Ext.QuickTips){ // fix for floating editors interacting with DND
536 Ext.QuickTips.enable();
539 clear: function(field){
540 field.el.removeClass(field.invalidClass);
541 field.el.dom.qtip = '';
545 mark: function(field, msg){
546 field.el.addClass(field.invalidClass);
547 field.el.dom.title = msg;
549 clear: function(field){
550 field.el.dom.title = '';
554 mark: function(field, msg){
555 field.el.addClass(field.invalidClass);
557 var elp = field.getErrorCt();
558 if(!elp){ // field has no container el
559 field.el.dom.title = msg;
562 field.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
563 field.errorEl.setWidth(elp.getWidth(true)-20);
565 field.errorEl.update(msg);
566 Ext.form.Field.msgFx[field.msgFx].show(field.errorEl, field);
568 clear: function(field){
569 field.el.removeClass(field.invalidClass);
571 Ext.form.Field.msgFx[field.msgFx].hide(field.errorEl, field);
573 field.el.dom.title = '';
578 mark: function(field, msg){
579 field.el.addClass(field.invalidClass);
580 if(!field.errorIcon){
581 var elp = field.getErrorCt();
582 if(!elp){ // field has no container el
583 field.el.dom.title = msg;
586 field.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
588 field.alignErrorIcon();
589 field.errorIcon.dom.qtip = msg;
590 field.errorIcon.dom.qclass = 'x-form-invalid-tip';
591 field.errorIcon.show();
592 field.on('resize', field.alignErrorIcon, field);
594 clear: function(field){
595 field.el.removeClass(field.invalidClass);
597 field.errorIcon.dom.qtip = '';
598 field.errorIcon.hide();
599 field.un('resize', field.alignErrorIcon, field);
601 field.el.dom.title = '';
607 // anything other than normal should be considered experimental
608 Ext.form.Field.msgFx = {
610 show: function(msgEl, f){
611 msgEl.setDisplayed('block');
614 hide : function(msgEl, f){
615 msgEl.setDisplayed(false).update('');
620 show: function(msgEl, f){
621 msgEl.slideIn('t', {stopFx:true});
624 hide : function(msgEl, f){
625 msgEl.slideOut('t', {stopFx:true,useDisplay:true});
630 show: function(msgEl, f){
632 msgEl.alignTo(f.el, 'tl-tr');
633 msgEl.slideIn('l', {stopFx:true});
636 hide : function(msgEl, f){
637 msgEl.slideOut('l', {stopFx:true,useDisplay:true});
641 Ext.reg('field', Ext.form.Field);