1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-form.field.Date-method-constructor'><span id='Ext-form.field.Date'>/**
2 </span></span> * @class Ext.form.field.Date
3 * @extends Ext.form.field.Picker
5 Provides a date input field with a {@link Ext.picker.Date date picker} dropdown and automatic date
8 This field recognizes and uses the JavaScript Date object as its main {@link #value} type. In addition,
9 it recognizes string values which are parsed according to the {@link #format} and/or {@link #altFormats}
10 configs. These may be reconfigured to use date formats appropriate for the user's locale.
12 The field may be limited to a certain range of dates by using the {@link #minValue}, {@link #maxValue},
13 {@link #disabledDays}, and {@link #disabledDates} config parameters. These configurations will be used both
14 in the field's validation, and in the date picker dropdown by preventing invalid dates from being selected.
15 {@img Ext.form.Date/Ext.form.Date.png Ext.form.Date component}
18 Ext.create('Ext.form.Panel', {
27 maxValue: new Date() // limited to the current date or prior
33 value: new Date() // defaults to today
35 renderTo: Ext.getBody()
38 #Date Formats Examples#
40 This example shows a couple of different date format parsing scenarios. Both use custom date format
41 configurations; the first one matches the configured `format` while the second matches the `altFormats`.
43 Ext.create('Ext.form.Panel', {
44 renderTo: Ext.getBody(),
53 // The value matches the format; will be parsed and displayed using that format.
61 // The value does not match the format, but does match an altFormat; will be parsed
62 // using the altFormat and displayed using the format.
64 altFormats: 'm,d,Y|m.d.Y',
70 * Create a new Date field
71 * @param {Object} config
75 * @docauthor Jason Johnston <jason@sencha.com>
77 Ext.define('Ext.form.field.Date', {
78 extend:'Ext.form.field.Picker',
79 alias: 'widget.datefield',
80 requires: ['Ext.picker.Date'],
81 alternateClassName: ['Ext.form.DateField', 'Ext.form.Date'],
83 <span id='Ext-form.field.Date-cfg-format'> /**
84 </span> * @cfg {String} format
85 * The default date format string which can be overriden for localization support. The format must be
86 * valid according to {@link Ext.Date#parse} (defaults to <tt>'m/d/Y'</tt>).
88 format : "m/d/Y",
89 <span id='Ext-form.field.Date-cfg-altFormats'> /**
90 </span> * @cfg {String} altFormats
91 * Multiple date formats separated by "<tt>|</tt>" to try when parsing a user input value and it
92 * does not match the defined format (defaults to
93 * <tt>'m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j'</tt>).
95 altFormats : "m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j",
96 <span id='Ext-form.field.Date-cfg-disabledDaysText'> /**
97 </span> * @cfg {String} disabledDaysText
98 * The tooltip to display when the date falls on a disabled day (defaults to <tt>'Disabled'</tt>)
100 disabledDaysText : "Disabled",
101 <span id='Ext-form.field.Date-cfg-disabledDatesText'> /**
102 </span> * @cfg {String} disabledDatesText
103 * The tooltip text to display when the date falls on a disabled date (defaults to <tt>'Disabled'</tt>)
105 disabledDatesText : "Disabled",
106 <span id='Ext-form.field.Date-cfg-minText'> /**
107 </span> * @cfg {String} minText
108 * The error text to display when the date in the cell is before <tt>{@link #minValue}</tt> (defaults to
109 * <tt>'The date in this field must be after {minValue}'</tt>).
111 minText : "The date in this field must be equal to or after {0}",
112 <span id='Ext-form.field.Date-cfg-maxText'> /**
113 </span> * @cfg {String} maxText
114 * The error text to display when the date in the cell is after <tt>{@link #maxValue}</tt> (defaults to
115 * <tt>'The date in this field must be before {maxValue}'</tt>).
117 maxText : "The date in this field must be equal to or before {0}",
118 <span id='Ext-form.field.Date-cfg-invalidText'> /**
119 </span> * @cfg {String} invalidText
120 * The error text to display when the date in the field is invalid (defaults to
121 * <tt>'{value} is not a valid date - it must be in the format {format}'</tt>).
123 invalidText : "{0} is not a valid date - it must be in the format {1}",
124 <span id='Ext-form.field.Date-cfg-triggerCls'> /**
125 </span> * @cfg {String} triggerCls
126 * An additional CSS class used to style the trigger button. The trigger will always get the
127 * class <tt>'x-form-trigger'</tt> and <tt>triggerCls</tt> will be <b>appended</b> if specified
128 * (defaults to <tt>'x-form-date-trigger'</tt> which displays a calendar icon).
130 triggerCls : Ext.baseCSSPrefix + 'form-date-trigger',
131 <span id='Ext-form.field.Date-cfg-showToday'> /**
132 </span> * @cfg {Boolean} showToday
133 * <tt>false</tt> to hide the footer area of the Date picker containing the Today button and disable
134 * the keyboard handler for spacebar that selects the current date (defaults to <tt>true</tt>).
137 <span id='Ext-form.field.Date-cfg-minValue'> /**
138 </span> * @cfg {Date/String} minValue
139 * The minimum allowed date. Can be either a Javascript date object or a string date in a
140 * valid format (defaults to undefined).
142 <span id='Ext-form.field.Date-cfg-maxValue'> /**
143 </span> * @cfg {Date/String} maxValue
144 * The maximum allowed date. Can be either a Javascript date object or a string date in a
145 * valid format (defaults to undefined).
147 <span id='Ext-form.field.Date-cfg-disabledDays'> /**
148 </span> * @cfg {Array} disabledDays
149 * An array of days to disable, 0 based (defaults to undefined). Some examples:<pre><code>
150 // disable Sunday and Saturday:
153 disabledDays: [1,2,3,4,5]
154 * </code></pre>
156 <span id='Ext-form.field.Date-cfg-disabledDates'> /**
157 </span> * @cfg {Array} disabledDates
158 * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
159 * expression so they are very powerful. Some examples:<pre><code>
160 // disable these exact dates:
161 disabledDates: ["03/08/2003", "09/16/2003"]
162 // disable these days for every year:
163 disabledDates: ["03/08", "09/16"]
164 // only match the beginning (useful if you are using short years):
165 disabledDates: ["^03/08"]
166 // disable every day in March 2006:
167 disabledDates: ["03/../2006"]
168 // disable every day in every March:
169 disabledDates: ["^03"]
170 * </code></pre>
171 * Note that the format of the dates included in the array should exactly match the {@link #format} config.
172 * In order to support regular expressions, if you are using a {@link #format date format} that has "." in
173 * it, you will have to escape the dot when restricting dates. For example: <tt>["03\\.08\\.03"]</tt>.
176 <span id='Ext-form.field.Date-cfg-submitFormat'> /**
177 </span> * @cfg {String} submitFormat The date format string which will be submitted to the server.
178 * The format must be valid according to {@link Ext.Date#parse} (defaults to <tt>{@link #format}</tt>).
181 // in the absence of a time value, a default value of 12 noon will be used
182 // (note: 12 noon was chosen because it steers well clear of all DST timezone changes)
183 initTime: '12', // 24 hour format
187 matchFieldWidth: false,
188 <span id='Ext-form.field.Date-cfg-startDay'> /**
189 </span> * @cfg {Number} startDay
190 * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
194 initComponent : function(){
196 isString = Ext.isString,
202 me.minValue = me.parseDate(min);
205 me.maxValue = me.parseDate(max);
207 me.disabledDatesRE = null;
208 me.initDisabledDays();
213 initValue: function() {
217 // If a String value was supplied, try to convert it to a proper Date
218 if (Ext.isString(value)) {
219 me.value = me.rawToValue(value);
226 initDisabledDays : function(){
227 if(this.disabledDates){
228 var dd = this.disabledDates,
230 re = "(?:";
232 Ext.each(dd, function(d, i){
233 re += Ext.isDate(d) ? '^' + Ext.String.escapeRegex(d.dateFormat(this.format)) + '$' : dd[i];
238 this.disabledDatesRE = new RegExp(re + ')');
242 <span id='Ext-form.field.Date-method-setDisabledDates'> /**
243 </span> * Replaces any existing disabled dates with new values and refreshes the Date picker.
244 * @param {Array} disabledDates An array of date strings (see the <tt>{@link #disabledDates}</tt> config
245 * for details on supported values) used to disable a pattern of dates.
247 setDisabledDates : function(dd){
251 me.disabledDates = dd;
252 me.initDisabledDays();
254 picker.setDisabledDates(me.disabledDatesRE);
258 <span id='Ext-form.field.Date-method-setDisabledDays'> /**
259 </span> * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the Date picker.
260 * @param {Array} disabledDays An array of disabled day indexes. See the <tt>{@link #disabledDays}</tt>
261 * config for details on supported values.
263 setDisabledDays : function(dd){
264 var picker = this.picker;
266 this.disabledDays = dd;
268 picker.setDisabledDays(dd);
272 <span id='Ext-form.field.Date-method-setMinValue'> /**
273 </span> * Replaces any existing <tt>{@link #minValue}</tt> with the new value and refreshes the Date picker.
274 * @param {Date} value The minimum date that can be selected
276 setMinValue : function(dt){
279 minValue = (Ext.isString(dt) ? me.parseDate(dt) : dt);
281 me.minValue = minValue;
283 picker.minText = Ext.String.format(me.minText, me.formatDate(me.minValue));
284 picker.setMinDate(minValue);
288 <span id='Ext-form.field.Date-method-setMaxValue'> /**
289 </span> * Replaces any existing <tt>{@link #maxValue}</tt> with the new value and refreshes the Date picker.
290 * @param {Date} value The maximum date that can be selected
292 setMaxValue : function(dt){
295 maxValue = (Ext.isString(dt) ? me.parseDate(dt) : dt);
297 me.maxValue = maxValue;
299 picker.maxText = Ext.String.format(me.maxText, me.formatDate(me.maxValue));
300 picker.setMaxDate(maxValue);
304 <span id='Ext-form.field.Date-method-getErrors'> /**
305 </span> * Runs all of Date's validations and returns an array of any errors. Note that this first
306 * runs Text's validations, so the returned array is an amalgamation of all field errors.
307 * The additional validation checks are testing that the date format is valid, that the chosen
308 * date is within the min and max date constraints set, that the date chosen is not in the disabledDates
309 * regex and that the day chosed is not one of the disabledDays.
310 * @param {Mixed} value The value to get errors for (defaults to the current field value)
311 * @return {Array} All validation errors for this field
313 getErrors: function(value) {
315 format = Ext.String.format,
316 clearTime = Ext.Date.clearTime,
317 errors = me.callParent(arguments),
318 disabledDays = me.disabledDays,
319 disabledDatesRE = me.disabledDatesRE,
320 minValue = me.minValue,
321 maxValue = me.maxValue,
322 len = disabledDays ? disabledDays.length : 0,
329 value = me.formatDate(value || me.processRawValue(me.getRawValue()));
331 if (value === null || value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
336 value = me.parseDate(value);
338 errors.push(format(me.invalidText, svalue, me.format));
342 time = value.getTime();
343 if (minValue && time < clearTime(minValue).getTime()) {
344 errors.push(format(me.minText, me.formatDate(minValue)));
347 if (maxValue && time > clearTime(maxValue).getTime()) {
348 errors.push(format(me.maxText, me.formatDate(maxValue)));
352 day = value.getDay();
354 for(; i < len; i++) {
355 if (day === disabledDays[i]) {
356 errors.push(me.disabledDaysText);
362 fvalue = me.formatDate(value);
363 if (disabledDatesRE && disabledDatesRE.test(fvalue)) {
364 errors.push(format(me.disabledDatesText, fvalue));
370 rawToValue: function(rawValue) {
371 return this.parseDate(rawValue) || rawValue || null;
374 valueToRaw: function(value) {
375 return this.formatDate(this.parseDate(value));
378 <span id='Ext-form.field.Date-method-setValue'> /**
379 </span> * Sets the value of the date field. You can pass a date object or any string that can be
380 * parsed into a valid date, using <tt>{@link #format}</tt> as the date format, according
381 * to the same rules as {@link Ext.Date#parse} (the default format used is <tt>"m/d/Y"</tt>).
383 * <pre><code>
384 //All of these calls set the same date value (May 4, 2006)
386 //Pass a date object:
387 var dt = new Date('5/4/2006');
388 dateField.setValue(dt);
390 //Pass a date string (default format):
391 dateField.setValue('05/04/2006');
393 //Pass a date string (custom format):
394 dateField.format = 'Y-m-d';
395 dateField.setValue('2006-05-04');
396 </code></pre>
397 * @param {String/Date} date The date or valid date string
398 * @return {Ext.form.field.Date} this
402 <span id='Ext-form.field.Date-method-safeParse'> /**
403 </span> * Attempts to parse a given string value using a given {@link Ext.Date#parse date format}.
404 * @param {String} value The value to attempt to parse
405 * @param {String} format A valid date format (see {@link Ext.Date#parse})
406 * @return {Date} The parsed Date object, or null if the value could not be successfully parsed.
408 safeParse : function(value, format) {
414 if (utilDate.formatContainsHourInfo(format)) {
415 // if parse format contains hour information, no DST adjustment is necessary
416 result = utilDate.parse(value, format);
418 // set time to 12 noon, then clear the time
419 parsedDate = utilDate.parse(value + ' ' + me.initTime, format + ' ' + me.initTimeFormat);
421 result = utilDate.clearTime(parsedDate);
428 getSubmitValue: function() {
430 format = me.submitFormat || me.format,
431 value = me.getValue();
433 return value ? Ext.Date.format(value, format) : null;
436 <span id='Ext-form.field.Date-method-parseDate'> /**
439 parseDate : function(value) {
440 if(!value || Ext.isDate(value)){
445 val = me.safeParse(value, me.format),
446 altFormats = me.altFormats,
447 altFormatsArray = me.altFormatsArray,
451 if (!val && altFormats) {
452 altFormatsArray = altFormatsArray || altFormats.split('|');
453 len = altFormatsArray.length;
454 for (; i < len && !val; ++i) {
455 val = me.safeParse(value, altFormatsArray[i]);
462 formatDate : function(date){
463 return Ext.isDate(date) ? Ext.Date.dateFormat(date, this.format) : date;
466 createPicker: function() {
468 format = Ext.String.format;
470 return Ext.create('Ext.picker.Date', {
471 ownerCt: this.ownerCt,
472 renderTo: document.body,
476 minDate: me.minValue,
477 maxDate: me.maxValue,
478 disabledDatesRE: me.disabledDatesRE,
479 disabledDatesText: me.disabledDatesText,
480 disabledDays: me.disabledDays,
481 disabledDaysText: me.disabledDaysText,
483 showToday: me.showToday,
484 startDay: me.startDay,
485 minText: format(me.minText, me.formatDate(me.minValue)),
486 maxText: format(me.maxText, me.formatDate(me.maxValue)),
499 onSelect: function(m, d) {
501 this.fireEvent('select', this, d);
505 <span id='Ext-form.field.Date-method-onExpand'> /**
507 * Sets the Date picker's value to match the current field value when expanding.
509 onExpand: function() {
511 value = me.getValue();
512 me.picker.setValue(value instanceof Date ? value : new Date());
515 <span id='Ext-form.field.Date-method-onCollapse'> /**
517 * Focuses the field when collapsing the Date picker.
519 onCollapse: function() {
520 this.focus(false, 60);
524 beforeBlur : function(){
525 var v = this.parseDate(this.getRawValue());
531 <span id='Ext-form.field.Date-cfg-grow'> /**
532 </span> * @cfg {Boolean} grow @hide
534 <span id='Ext-form.field.Date-cfg-growMin'> /**
535 </span> * @cfg {Number} growMin @hide
537 <span id='Ext-form.field.Date-cfg-growMax'> /**
538 </span> * @cfg {Number} growMax @hide
540 <span id='Ext-form.field.Date-method-autoSize'> /**
545 </pre></pre></body></html>