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-Text-method-constructor'><span id='Ext-form-field-Text'>/**
19 </span></span> * @class Ext.form.field.Text
20 * @extends Ext.form.field.Base
22 A basic text field. Can be used as a direct replacement for traditional text inputs,
23 or as the base class for more sophisticated input controls (like {@link Ext.form.field.TextArea}
24 and {@link Ext.form.field.ComboBox}). Has support for empty-field placeholder values (see {@link #emptyText}).
28 The Text field has a useful set of validations built in:
30 - {@link #allowBlank} for making the field required
31 - {@link #minLength} for requiring a minimum value length
32 - {@link #maxLength} for setting a maximum value length (with {@link #enforceMaxLength} to add it
33 as the `maxlength` attribute on the input element)
34 - {@link regex} to specify a custom regular expression for validation
36 In addition, custom validations may be added:
38 - {@link #vtype} specifies a virtual type implementation from {@link Ext.form.field.VTypes} which can contain
39 custom validation logic
40 - {@link #validator} allows a custom arbitrary function to be called during validation
42 The details around how and when each of these validation options get used are described in the
43 documentation for {@link #getErrors}.
45 By default, the field value is checked for validity immediately while the user is typing in the
46 field. This can be controlled with the {@link #validateOnChange}, {@link #checkChangeEvents}, and
47 {@link #checkChangeBugger} configurations. Also see the details on Form Validation in the
48 {@link Ext.form.Panel} class documentation.
50 #Masking and Character Stripping#
52 Text fields can be configured with custom regular expressions to be applied to entered values before
53 validation: see {@link #maskRe} and {@link #stripCharsRe} for details.
54 {@img Ext.form.Text/Ext.form.Text.png Ext.form.Text component}
57 Ext.create('Ext.form.Panel', {
58 title: 'Contact Info',
61 renderTo: Ext.getBody(),
66 allowBlank: false // requires a non-empty value
70 fieldLabel: 'Email Address',
71 vtype: 'email' // requires value to be a valid email address format
75 * @constructor Creates a new TextField
76 * @param {Object} config Configuration options
80 * @docauthor Jason Johnston <jason@sencha.com>
82 Ext.define('Ext.form.field.Text', {
83 extend:'Ext.form.field.Base',
84 alias: 'widget.textfield',
85 requires: ['Ext.form.field.VTypes', 'Ext.layout.component.field.Text'],
86 alternateClassName: ['Ext.form.TextField', 'Ext.form.Text'],
88 <span id='Ext-form-field-Text-cfg-vtypeText'> /**
89 </span> * @cfg {String} vtypeText A custom error message to display in place of the default message provided
90 * for the <b><code>{@link #vtype}</code></b> currently set for this field (defaults to <tt>undefined</tt>).
91 * <b>Note</b>: only applies if <b><code>{@link #vtype}</code></b> is set, else ignored.
94 <span id='Ext-form-field-Text-cfg-stripCharsRe'> /**
95 </span> * @cfg {RegExp} stripCharsRe A JavaScript RegExp object used to strip unwanted content from the value
96 * before validation (defaults to <tt>undefined</tt>).
99 <span id='Ext-form-field-Text-cfg-size'> /**
100 </span> * @cfg {Number} size An initial value for the 'size' attribute on the text input element. This is only
101 * used if the field has no configured {@link #width} and is not given a width by its container's layout.
102 * Defaults to <tt>20</tt>.
106 <span id='Ext-form-field-Text-cfg-grow'> /**
107 </span> * @cfg {Boolean} grow <tt>true</tt> if this field should automatically grow and shrink to its content
108 * (defaults to <tt>false</tt>)
111 <span id='Ext-form-field-Text-cfg-growMin'> /**
112 </span> * @cfg {Number} growMin The minimum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
113 * to <tt>30</tt>)
117 <span id='Ext-form-field-Text-cfg-growMax'> /**
118 </span> * @cfg {Number} growMax The maximum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
119 * to <tt>800</tt>)
123 <span id='Ext-form-field-Text-cfg-growAppend'> /**
124 </span> * @cfg {String} growAppend
125 * A string that will be appended to the field's current value for the purposes of calculating the target
126 * field size. Only used when the {@link #grow} config is <tt>true</tt>. Defaults to a single capital "W"
127 * (the widest character in common fonts) to leave enough space for the next typed character and avoid the
128 * field value shifting before the width is adjusted.
132 <span id='Ext-form-field-Text-cfg-vtype'> /**
133 </span> * @cfg {String} vtype A validation type name as defined in {@link Ext.form.field.VTypes} (defaults to <tt>undefined</tt>)
136 <span id='Ext-form-field-Text-cfg-maskRe'> /**
137 </span> * @cfg {RegExp} maskRe An input mask regular expression that will be used to filter keystrokes that do
138 * not match (defaults to <tt>undefined</tt>)
141 <span id='Ext-form-field-Text-cfg-disableKeyFilter'> /**
142 </span> * @cfg {Boolean} disableKeyFilter Specify <tt>true</tt> to disable input keystroke filtering (defaults
143 * to <tt>false</tt>)
146 <span id='Ext-form-field-Text-cfg-allowBlank'> /**
147 </span> * @cfg {Boolean} allowBlank Specify <tt>false</tt> to validate that the value's length is > 0 (defaults to
148 * <tt>true</tt>)
152 <span id='Ext-form-field-Text-cfg-minLength'> /**
153 </span> * @cfg {Number} minLength Minimum input field length required (defaults to <tt>0</tt>)
157 <span id='Ext-form-field-Text-cfg-maxLength'> /**
158 </span> * @cfg {Number} maxLength Maximum input field length allowed by validation (defaults to Number.MAX_VALUE).
159 * This behavior is intended to provide instant feedback to the user by improving usability to allow pasting
160 * and editing or overtyping and back tracking. To restrict the maximum number of characters that can be
161 * entered into the field use the <tt><b>{@link Ext.form.field.Text#enforceMaxLength enforceMaxLength}</b></tt> option.
163 maxLength : Number.MAX_VALUE,
165 <span id='Ext-form-field-Text-cfg-enforceMaxLength'> /**
166 </span> * @cfg {Boolean} enforceMaxLength True to set the maxLength property on the underlying input field. Defaults to <tt>false</tt>
169 <span id='Ext-form-field-Text-cfg-minLengthText'> /**
170 </span> * @cfg {String} minLengthText Error text to display if the <b><tt>{@link #minLength minimum length}</tt></b>
171 * validation fails (defaults to <tt>'The minimum length for this field is {minLength}'</tt>)
173 minLengthText : 'The minimum length for this field is {0}',
175 <span id='Ext-form-field-Text-cfg-maxLengthText'> /**
176 </span> * @cfg {String} maxLengthText Error text to display if the <b><tt>{@link #maxLength maximum length}</tt></b>
177 * validation fails (defaults to <tt>'The maximum length for this field is {maxLength}'</tt>)
179 maxLengthText : 'The maximum length for this field is {0}',
181 <span id='Ext-form-field-Text-cfg-selectOnFocus'> /**
182 </span> * @cfg {Boolean} selectOnFocus <tt>true</tt> to automatically select any existing field text when the field
183 * receives input focus (defaults to <tt>false</tt>)
186 <span id='Ext-form-field-Text-cfg-blankText'> /**
187 </span> * @cfg {String} blankText The error text to display if the <b><tt>{@link #allowBlank}</tt></b> validation
188 * fails (defaults to <tt>'This field is required'</tt>)
190 blankText : 'This field is required',
192 <span id='Ext-form-field-Text-cfg-validator'> /**
193 </span> * @cfg {Function} validator
194 * <p>A custom validation function to be called during field validation ({@link #getErrors})
195 * (defaults to <tt>undefined</tt>). If specified, this function will be called first, allowing the
196 * developer to override the default validation process.</p>
197 * <br><p>This function will be passed the following Parameters:</p>
198 * <div class="mdetail-params"><ul>
199 * <li><code>value</code>: <i>Mixed</i>
200 * <div class="sub-desc">The current field value</div></li>
201 * </ul></div>
202 * <br><p>This function is to Return:</p>
203 * <div class="mdetail-params"><ul>
204 * <li><code>true</code>: <i>Boolean</i>
205 * <div class="sub-desc"><code>true</code> if the value is valid</div></li>
206 * <li><code>msg</code>: <i>String</i>
207 * <div class="sub-desc">An error message if the value is invalid</div></li>
208 * </ul></div>
211 <span id='Ext-form-field-Text-cfg-regex'> /**
212 </span> * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation
213 * (defaults to <tt>undefined</tt>). If the test fails, the field will be marked invalid using
214 * <b><tt>{@link #regexText}</tt></b>.
217 <span id='Ext-form-field-Text-cfg-regexText'> /**
218 </span> * @cfg {String} regexText The error text to display if <b><tt>{@link #regex}</tt></b> is used and the
219 * test fails during validation (defaults to <tt>''</tt>)
223 <span id='Ext-form-field-Text-cfg-emptyText'> /**
224 </span> * @cfg {String} emptyText
225 * <p>The default text to place into an empty field (defaults to <tt>undefined</tt>).</p>
226 * <p>Note that normally this value will be submitted to the server if this field is enabled; to prevent this
227 * you can set the {@link Ext.form.action.Action#submitEmptyText submitEmptyText} option of
228 * {@link Ext.form.Basic#submit} to <tt>false</tt>.</p>
229 * <p>Also note that if you use <tt>{@link #inputType inputType}:'file'</tt>, {@link #emptyText} is not
230 * supported and should be avoided.</p>
233 <span id='Ext-form-field-Text-cfg-emptyCls'> /**
234 </span> * @cfg {String} emptyCls The CSS class to apply to an empty field to style the <b><tt>{@link #emptyText}</tt></b>
235 * (defaults to <tt>'x-form-empty-field'</tt>). This class is automatically added and removed as needed
236 * depending on the current field value.
238 emptyCls : Ext.baseCSSPrefix + 'form-empty-field',
242 <span id='Ext-form-field-Text-cfg-enableKeyEvents'> /**
243 </span> * @cfg {Boolean} enableKeyEvents <tt>true</tt> to enable the proxying of key events for the HTML input field (defaults to <tt>false</tt>)
246 componentLayout: 'textfield',
248 initComponent : function(){
251 <span id='Ext-form-field-Text-event-autosize'> /**
252 </span> * @event autosize
253 * Fires when the <tt><b>{@link #autoSize}</b></tt> function is triggered and the field is
254 * resized according to the {@link #grow}/{@link #growMin}/{@link #growMax} configs as a result.
255 * This event provides a hook for the developer to apply additional logic at runtime to resize the
257 * @param {Ext.form.field.Text} this This text field
258 * @param {Number} width The new field width
262 <span id='Ext-form-field-Text-event-keydown'> /**
263 </span> * @event keydown
264 * Keydown input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
266 * @param {Ext.form.field.Text} this This text field
267 * @param {Ext.EventObject} e
270 <span id='Ext-form-field-Text-event-keyup'> /**
271 </span> * @event keyup
272 * Keyup input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
274 * @param {Ext.form.field.Text} this This text field
275 * @param {Ext.EventObject} e
278 <span id='Ext-form-field-Text-event-keypress'> /**
279 </span> * @event keypress
280 * Keypress input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
282 * @param {Ext.form.field.Text} this This text field
283 * @param {Ext.EventObject} e
290 initEvents : function(){
295 if(me.selectOnFocus || me.emptyText){
296 me.mon(el, 'mousedown', me.onMouseDown, me);
298 if(me.maskRe || (me.vtype && me.disableKeyFilter !== true && (me.maskRe = Ext.form.field.VTypes[me.vtype+'Mask']))){
299 me.mon(el, 'keypress', me.filterKeys, me);
302 if (me.enableKeyEvents) {
306 keydown: me.onKeyDown,
307 keypress: me.onKeyPress
312 <span id='Ext-form-field-Text-method-isEqual'> /**
313 </span> * @private override - treat undefined and null values as equal to an empty string value
315 isEqual: function(value1, value2) {
316 return String(Ext.value(value1, '')) === String(Ext.value(value2, ''));
319 <span id='Ext-form-field-Text-method-onChange'> /**
321 * If grow=true, invoke the autoSize method when the field's value is changed.
323 onChange: function() {
328 afterRender: function(){
330 if (me.enforceMaxLength) {
331 me.inputEl.dom.maxLength = me.maxLength;
338 onMouseDown: function(e){
341 me.mon(me.inputEl, 'mouseup', Ext.emptyFn, me, { single: true, preventDefault: true });
345 <span id='Ext-form-field-Text-method-processRawValue'> /**
346 </span> * Performs any necessary manipulation of a raw String value to prepare it for {@link #stringToValue conversion}
347 * and/or {@link #validate validation}. For text fields this applies the configured {@link #stripCharsRe} to the
349 * @param {String} value The unprocessed string value
350 * @return {String} The processed string value
352 processRawValue: function(value) {
354 stripRe = me.stripCharsRe,
358 newValue = value.replace(stripRe, '');
359 if (newValue !== value) {
360 me.setRawValue(newValue);
368 onDisable: function(){
371 this.inputEl.dom.unselectable = 'on';
376 onEnable: function(){
379 this.inputEl.dom.unselectable = '';
383 onKeyDown: function(e) {
384 this.fireEvent('keydown', this, e);
387 onKeyUp: function(e) {
388 this.fireEvent('keyup', this, e);
391 onKeyPress: function(e) {
392 this.fireEvent('keypress', this, e);
395 <span id='Ext-form-field-Text-method-reset'> /**
396 </span> * Resets the current field value to the originally-loaded value and clears any validation messages.
397 * Also adds <tt><b>{@link #emptyText}</b></tt> and <tt><b>{@link #emptyCls}</b></tt> if the
398 * original value was blank.
402 this.applyEmptyText();
405 applyEmptyText : function(){
407 emptyText = me.emptyText,
410 if (me.rendered && emptyText) {
411 isEmpty = me.getRawValue().length < 1 && !me.hasFocus;
413 if (Ext.supports.Placeholder) {
414 me.inputEl.dom.placeholder = emptyText;
415 } else if (isEmpty) {
416 me.setRawValue(emptyText);
419 //all browsers need this because of a styling issue with chrome + placeholders.
420 //the text isnt vertically aligned when empty (and using the placeholder)
422 me.inputEl.addCls(me.emptyCls);
430 preFocus : function(){
432 inputEl = me.inputEl,
433 emptyText = me.emptyText,
436 if (emptyText && !Ext.supports.Placeholder && inputEl.dom.value === emptyText) {
439 inputEl.removeCls(me.emptyCls);
440 } else if (Ext.supports.Placeholder) {
441 me.inputEl.removeCls(me.emptyCls);
443 if (me.selectOnFocus || isEmpty) {
444 inputEl.dom.select();
448 onFocus: function() {
450 me.callParent(arguments);
457 postBlur : function(){
458 this.applyEmptyText();
462 filterKeys : function(e){
466 var key = e.getKey(),
467 charCode = String.fromCharCode(e.getCharCode());
469 if(Ext.isGecko && (e.isNavKeyPress() || key === e.BACKSPACE || (key === e.DELETE && e.button === -1))){
473 if(!Ext.isGecko && e.isSpecialKey() && !charCode){
476 if(!this.maskRe.test(charCode)){
481 <span id='Ext-form-field-Text-method-getRawValue'> /**
482 </span> * Returns the raw String value of the field, without performing any normalization, conversion, or validation.
483 * Gets the current value of the input element if the field has been rendered, ignoring the value if it is the
484 * {@link #emptyText}. To get a normalized and converted value see {@link #getValue}.
485 * @return {String} value The raw String value of the field
487 getRawValue: function() {
490 if (v === me.emptyText) {
496 <span id='Ext-form-field-Text-method-setValue'> /**
497 </span> * Sets a data value into the field and runs the change detection and validation. Also applies any configured
498 * {@link #emptyText} for text fields. To set the value directly without these inspections see {@link #setRawValue}.
499 * @param {Mixed} value The value to set
500 * @return {Ext.form.field.Text} this
502 setValue: function(value) {
504 inputEl = me.inputEl;
506 if (inputEl && me.emptyText && !Ext.isEmpty(value)) {
507 inputEl.removeCls(me.emptyCls);
510 me.callParent(arguments);
516 <span id='Ext-form-field-Text-method-getErrors'> /**
517 </span>Validates a value according to the field's validation rules and returns an array of errors
518 for any failing validations. Validation rules are processed in the following order:
520 1. **Field specific validator**
522 A validator offers a way to customize and reuse a validation specification.
523 If a field is configured with a `{@link #validator}`
524 function, it will be passed the current field value. The `{@link #validator}`
525 function is expected to return either:
527 - Boolean `true` if the value is valid (validation continues).
528 - a String to represent the invalid message if invalid (validation halts).
530 2. **Basic Validation**
532 If the `{@link #validator}` has not halted validation,
533 basic validation proceeds as follows:
535 - `{@link #allowBlank}` : (Invalid message = `{@link #emptyText}`)
537 Depending on the configuration of <code>{@link #allowBlank}</code>, a
538 blank field will cause validation to halt at this step and return
539 Boolean true or false accordingly.
541 - `{@link #minLength}` : (Invalid message = `{@link #minLengthText}`)
543 If the passed value does not satisfy the `{@link #minLength}`
544 specified, validation halts.
546 - `{@link #maxLength}` : (Invalid message = `{@link #maxLengthText}`)
548 If the passed value does not satisfy the `{@link #maxLength}`
549 specified, validation halts.
551 3. **Preconfigured Validation Types (VTypes)**
553 If none of the prior validation steps halts validation, a field
554 configured with a `{@link #vtype}` will utilize the
555 corresponding {@link Ext.form.field.VTypes VTypes} validation function.
556 If invalid, either the field's `{@link #vtypeText}` or
557 the VTypes vtype Text property will be used for the invalid message.
558 Keystrokes on the field will be filtered according to the VTypes
561 4. **Field specific regex test**
563 If none of the prior validation steps halts validation, a field's
564 configured <code>{@link #regex}</code> test will be processed.
565 The invalid message for this test is configured with `{@link #regexText}`
567 * @param {Mixed} value The value to validate. The processed raw value will be used if nothing is passed
568 * @return {Array} Array of any validation errors
571 getErrors: function(value) {
573 errors = me.callParent(arguments),
574 validator = me.validator,
575 emptyText = me.emptyText,
576 allowBlank = me.allowBlank,
578 vtypes = Ext.form.field.VTypes,
580 format = Ext.String.format,
583 value = value || me.processRawValue(me.getRawValue());
585 if (Ext.isFunction(validator)) {
586 msg = validator.call(me, value);
592 if (value.length < 1 || value === emptyText) {
594 errors.push(me.blankText);
596 //if value is blank, there cannot be any additional errors
600 if (value.length < me.minLength) {
601 errors.push(format(me.minLengthText, me.minLength));
604 if (value.length > me.maxLength) {
605 errors.push(format(me.maxLengthText, me.maxLength));
609 if(!vtypes[vtype](value, me)){
610 errors.push(me.vtypeText || vtypes[vtype +'Text']);
614 if (regex && !regex.test(value)) {
615 errors.push(me.regexText || me.invalidText);
621 <span id='Ext-form-field-Text-method-selectText'> /**
622 </span> * Selects text in this field
623 * @param {Number} start (optional) The index where the selection should start (defaults to 0)
624 * @param {Number} end (optional) The index where the selection should end (defaults to the text length)
626 selectText : function(start, end){
628 v = me.getRawValue(),
634 if (v.length > 0) {
635 start = start === undef ? 0 : start;
636 end = end === undef ? v.length : end;
637 if (el.setSelectionRange) {
638 el.setSelectionRange(start, end);
640 else if(el.createTextRange) {
641 range = el.createTextRange();
642 range.moveStart('character', start);
643 range.moveEnd('character', end - v.length);
646 doFocus = Ext.isGecko || Ext.isOpera;
653 <span id='Ext-form-field-Text-method-autoSize'> /**
654 </span> * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed.
655 * This only takes effect if <tt>{@link #grow} = true</tt>, and fires the {@link #autosize} event if the
658 autoSize: function() {
661 if (me.grow && me.rendered) {
662 me.doComponentLayout();
663 width = me.inputEl.getWidth();
664 if (width !== me.lastInputWidth) {
665 me.fireEvent('autosize', width);
666 me.lastInputWidth = width;
671 initAria: function() {
673 this.getActionEl().dom.setAttribute('aria-required', this.allowBlank === false);
676 <span id='Ext-form-field-Text-method-getBodyNaturalWidth'> /**
677 </span> * @protected override
678 * To get the natural width of the inputEl, we do a simple calculation based on the
679 * 'size' config. We use hard-coded numbers to approximate what browsers do natively,
680 * to avoid having to read any styles which would hurt performance.
682 getBodyNaturalWidth: function() {
683 return Math.round(this.size * 6.5) + 20;