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-Date-method-constructor'><span id='Ext-form-field-Date'>/**
19 </span></span> * @class Ext.form.field.Date
20 * @extends Ext.form.field.Picker
22 Provides a date input field with a {@link Ext.picker.Date date picker} dropdown and automatic date
25 This field recognizes and uses the JavaScript Date object as its main {@link #value} type. In addition,
26 it recognizes string values which are parsed according to the {@link #format} and/or {@link #altFormats}
27 configs. These may be reconfigured to use date formats appropriate for the user's locale.
29 The field may be limited to a certain range of dates by using the {@link #minValue}, {@link #maxValue},
30 {@link #disabledDays}, and {@link #disabledDates} config parameters. These configurations will be used both
31 in the field's validation, and in the date picker dropdown by preventing invalid dates from being selected.
32 {@img Ext.form.Date/Ext.form.Date.png Ext.form.Date component}
35 Ext.create('Ext.form.Panel', {
44 maxValue: new Date() // limited to the current date or prior
50 value: new Date() // defaults to today
52 renderTo: Ext.getBody()
55 #Date Formats Examples#
57 This example shows a couple of different date format parsing scenarios. Both use custom date format
58 configurations; the first one matches the configured `format` while the second matches the `altFormats`.
60 Ext.create('Ext.form.Panel', {
61 renderTo: Ext.getBody(),
70 // The value matches the format; will be parsed and displayed using that format.
78 // The value does not match the format, but does match an altFormat; will be parsed
79 // using the altFormat and displayed using the format.
81 altFormats: 'm,d,Y|m.d.Y',
87 * Create a new Date field
88 * @param {Object} config
92 * @docauthor Jason Johnston <jason@sencha.com>
94 Ext.define('Ext.form.field.Date', {
95 extend:'Ext.form.field.Picker',
96 alias: 'widget.datefield',
97 requires: ['Ext.picker.Date'],
98 alternateClassName: ['Ext.form.DateField', 'Ext.form.Date'],
100 <span id='Ext-form-field-Date-cfg-format'> /**
101 </span> * @cfg {String} format
102 * The default date format string which can be overriden for localization support. The format must be
103 * valid according to {@link Ext.Date#parse} (defaults to <tt>'m/d/Y'</tt>).
105 format : "m/d/Y",
106 <span id='Ext-form-field-Date-cfg-altFormats'> /**
107 </span> * @cfg {String} altFormats
108 * Multiple date formats separated by "<tt>|</tt>" to try when parsing a user input value and it
109 * does not match the defined format (defaults to
110 * <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>).
112 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",
113 <span id='Ext-form-field-Date-cfg-disabledDaysText'> /**
114 </span> * @cfg {String} disabledDaysText
115 * The tooltip to display when the date falls on a disabled day (defaults to <tt>'Disabled'</tt>)
117 disabledDaysText : "Disabled",
118 <span id='Ext-form-field-Date-cfg-disabledDatesText'> /**
119 </span> * @cfg {String} disabledDatesText
120 * The tooltip text to display when the date falls on a disabled date (defaults to <tt>'Disabled'</tt>)
122 disabledDatesText : "Disabled",
123 <span id='Ext-form-field-Date-cfg-minText'> /**
124 </span> * @cfg {String} minText
125 * The error text to display when the date in the cell is before <tt>{@link #minValue}</tt> (defaults to
126 * <tt>'The date in this field must be after {minValue}'</tt>).
128 minText : "The date in this field must be equal to or after {0}",
129 <span id='Ext-form-field-Date-cfg-maxText'> /**
130 </span> * @cfg {String} maxText
131 * The error text to display when the date in the cell is after <tt>{@link #maxValue}</tt> (defaults to
132 * <tt>'The date in this field must be before {maxValue}'</tt>).
134 maxText : "The date in this field must be equal to or before {0}",
135 <span id='Ext-form-field-Date-cfg-invalidText'> /**
136 </span> * @cfg {String} invalidText
137 * The error text to display when the date in the field is invalid (defaults to
138 * <tt>'{value} is not a valid date - it must be in the format {format}'</tt>).
140 invalidText : "{0} is not a valid date - it must be in the format {1}",
141 <span id='Ext-form-field-Date-cfg-triggerCls'> /**
142 </span> * @cfg {String} triggerCls
143 * An additional CSS class used to style the trigger button. The trigger will always get the
144 * class <tt>'x-form-trigger'</tt> and <tt>triggerCls</tt> will be <b>appended</b> if specified
145 * (defaults to <tt>'x-form-date-trigger'</tt> which displays a calendar icon).
147 triggerCls : Ext.baseCSSPrefix + 'form-date-trigger',
148 <span id='Ext-form-field-Date-cfg-showToday'> /**
149 </span> * @cfg {Boolean} showToday
150 * <tt>false</tt> to hide the footer area of the Date picker containing the Today button and disable
151 * the keyboard handler for spacebar that selects the current date (defaults to <tt>true</tt>).
154 <span id='Ext-form-field-Date-cfg-minValue'> /**
155 </span> * @cfg {Date/String} minValue
156 * The minimum allowed date. Can be either a Javascript date object or a string date in a
157 * valid format (defaults to undefined).
159 <span id='Ext-form-field-Date-cfg-maxValue'> /**
160 </span> * @cfg {Date/String} maxValue
161 * The maximum allowed date. Can be either a Javascript date object or a string date in a
162 * valid format (defaults to undefined).
164 <span id='Ext-form-field-Date-cfg-disabledDays'> /**
165 </span> * @cfg {Array} disabledDays
166 * An array of days to disable, 0 based (defaults to undefined). Some examples:<pre><code>
167 // disable Sunday and Saturday:
170 disabledDays: [1,2,3,4,5]
171 * </code></pre>
173 <span id='Ext-form-field-Date-cfg-disabledDates'> /**
174 </span> * @cfg {Array} disabledDates
175 * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
176 * expression so they are very powerful. Some examples:<pre><code>
177 // disable these exact dates:
178 disabledDates: ["03/08/2003", "09/16/2003"]
179 // disable these days for every year:
180 disabledDates: ["03/08", "09/16"]
181 // only match the beginning (useful if you are using short years):
182 disabledDates: ["^03/08"]
183 // disable every day in March 2006:
184 disabledDates: ["03/../2006"]
185 // disable every day in every March:
186 disabledDates: ["^03"]
187 * </code></pre>
188 * Note that the format of the dates included in the array should exactly match the {@link #format} config.
189 * In order to support regular expressions, if you are using a {@link #format date format} that has "." in
190 * it, you will have to escape the dot when restricting dates. For example: <tt>["03\\.08\\.03"]</tt>.
193 <span id='Ext-form-field-Date-cfg-submitFormat'> /**
194 </span> * @cfg {String} submitFormat The date format string which will be submitted to the server.
195 * The format must be valid according to {@link Ext.Date#parse} (defaults to <tt>{@link #format}</tt>).
198 // in the absence of a time value, a default value of 12 noon will be used
199 // (note: 12 noon was chosen because it steers well clear of all DST timezone changes)
200 initTime: '12', // 24 hour format
204 matchFieldWidth: false,
205 <span id='Ext-form-field-Date-cfg-startDay'> /**
206 </span> * @cfg {Number} startDay
207 * Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
211 initComponent : function(){
213 isString = Ext.isString,
219 me.minValue = me.parseDate(min);
222 me.maxValue = me.parseDate(max);
224 me.disabledDatesRE = null;
225 me.initDisabledDays();
230 initValue: function() {
234 // If a String value was supplied, try to convert it to a proper Date
235 if (Ext.isString(value)) {
236 me.value = me.rawToValue(value);
243 initDisabledDays : function(){
244 if(this.disabledDates){
245 var dd = this.disabledDates,
247 re = "(?:";
249 Ext.each(dd, function(d, i){
250 re += Ext.isDate(d) ? '^' + Ext.String.escapeRegex(d.dateFormat(this.format)) + '$' : dd[i];
255 this.disabledDatesRE = new RegExp(re + ')');
259 <span id='Ext-form-field-Date-method-setDisabledDates'> /**
260 </span> * Replaces any existing disabled dates with new values and refreshes the Date picker.
261 * @param {Array} disabledDates An array of date strings (see the <tt>{@link #disabledDates}</tt> config
262 * for details on supported values) used to disable a pattern of dates.
264 setDisabledDates : function(dd){
268 me.disabledDates = dd;
269 me.initDisabledDays();
271 picker.setDisabledDates(me.disabledDatesRE);
275 <span id='Ext-form-field-Date-method-setDisabledDays'> /**
276 </span> * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the Date picker.
277 * @param {Array} disabledDays An array of disabled day indexes. See the <tt>{@link #disabledDays}</tt>
278 * config for details on supported values.
280 setDisabledDays : function(dd){
281 var picker = this.picker;
283 this.disabledDays = dd;
285 picker.setDisabledDays(dd);
289 <span id='Ext-form-field-Date-method-setMinValue'> /**
290 </span> * Replaces any existing <tt>{@link #minValue}</tt> with the new value and refreshes the Date picker.
291 * @param {Date} value The minimum date that can be selected
293 setMinValue : function(dt){
296 minValue = (Ext.isString(dt) ? me.parseDate(dt) : dt);
298 me.minValue = minValue;
300 picker.minText = Ext.String.format(me.minText, me.formatDate(me.minValue));
301 picker.setMinDate(minValue);
305 <span id='Ext-form-field-Date-method-setMaxValue'> /**
306 </span> * Replaces any existing <tt>{@link #maxValue}</tt> with the new value and refreshes the Date picker.
307 * @param {Date} value The maximum date that can be selected
309 setMaxValue : function(dt){
312 maxValue = (Ext.isString(dt) ? me.parseDate(dt) : dt);
314 me.maxValue = maxValue;
316 picker.maxText = Ext.String.format(me.maxText, me.formatDate(me.maxValue));
317 picker.setMaxDate(maxValue);
321 <span id='Ext-form-field-Date-method-getErrors'> /**
322 </span> * Runs all of Date's validations and returns an array of any errors. Note that this first
323 * runs Text's validations, so the returned array is an amalgamation of all field errors.
324 * The additional validation checks are testing that the date format is valid, that the chosen
325 * date is within the min and max date constraints set, that the date chosen is not in the disabledDates
326 * regex and that the day chosed is not one of the disabledDays.
327 * @param {Mixed} value The value to get errors for (defaults to the current field value)
328 * @return {Array} All validation errors for this field
330 getErrors: function(value) {
332 format = Ext.String.format,
333 clearTime = Ext.Date.clearTime,
334 errors = me.callParent(arguments),
335 disabledDays = me.disabledDays,
336 disabledDatesRE = me.disabledDatesRE,
337 minValue = me.minValue,
338 maxValue = me.maxValue,
339 len = disabledDays ? disabledDays.length : 0,
346 value = me.formatDate(value || me.processRawValue(me.getRawValue()));
348 if (value === null || value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
353 value = me.parseDate(value);
355 errors.push(format(me.invalidText, svalue, me.format));
359 time = value.getTime();
360 if (minValue && time < clearTime(minValue).getTime()) {
361 errors.push(format(me.minText, me.formatDate(minValue)));
364 if (maxValue && time > clearTime(maxValue).getTime()) {
365 errors.push(format(me.maxText, me.formatDate(maxValue)));
369 day = value.getDay();
371 for(; i < len; i++) {
372 if (day === disabledDays[i]) {
373 errors.push(me.disabledDaysText);
379 fvalue = me.formatDate(value);
380 if (disabledDatesRE && disabledDatesRE.test(fvalue)) {
381 errors.push(format(me.disabledDatesText, fvalue));
387 rawToValue: function(rawValue) {
388 return this.parseDate(rawValue) || rawValue || null;
391 valueToRaw: function(value) {
392 return this.formatDate(this.parseDate(value));
395 <span id='Ext-form-field-Date-method-setValue'> /**
396 </span> * Sets the value of the date field. You can pass a date object or any string that can be
397 * parsed into a valid date, using <tt>{@link #format}</tt> as the date format, according
398 * to the same rules as {@link Ext.Date#parse} (the default format used is <tt>"m/d/Y"</tt>).
400 * <pre><code>
401 //All of these calls set the same date value (May 4, 2006)
403 //Pass a date object:
404 var dt = new Date('5/4/2006');
405 dateField.setValue(dt);
407 //Pass a date string (default format):
408 dateField.setValue('05/04/2006');
410 //Pass a date string (custom format):
411 dateField.format = 'Y-m-d';
412 dateField.setValue('2006-05-04');
413 </code></pre>
414 * @param {String/Date} date The date or valid date string
415 * @return {Ext.form.field.Date} this
419 <span id='Ext-form-field-Date-method-safeParse'> /**
420 </span> * Attempts to parse a given string value using a given {@link Ext.Date#parse date format}.
421 * @param {String} value The value to attempt to parse
422 * @param {String} format A valid date format (see {@link Ext.Date#parse})
423 * @return {Date} The parsed Date object, or null if the value could not be successfully parsed.
425 safeParse : function(value, format) {
431 if (utilDate.formatContainsHourInfo(format)) {
432 // if parse format contains hour information, no DST adjustment is necessary
433 result = utilDate.parse(value, format);
435 // set time to 12 noon, then clear the time
436 parsedDate = utilDate.parse(value + ' ' + me.initTime, format + ' ' + me.initTimeFormat);
438 result = utilDate.clearTime(parsedDate);
445 getSubmitValue: function() {
447 format = me.submitFormat || me.format,
448 value = me.getValue();
450 return value ? Ext.Date.format(value, format) : null;
453 <span id='Ext-form-field-Date-method-parseDate'> /**
456 parseDate : function(value) {
457 if(!value || Ext.isDate(value)){
462 val = me.safeParse(value, me.format),
463 altFormats = me.altFormats,
464 altFormatsArray = me.altFormatsArray,
468 if (!val && altFormats) {
469 altFormatsArray = altFormatsArray || altFormats.split('|');
470 len = altFormatsArray.length;
471 for (; i < len && !val; ++i) {
472 val = me.safeParse(value, altFormatsArray[i]);
479 formatDate : function(date){
480 return Ext.isDate(date) ? Ext.Date.dateFormat(date, this.format) : date;
483 createPicker: function() {
485 format = Ext.String.format;
487 return Ext.create('Ext.picker.Date', {
489 renderTo: document.body,
493 minDate: me.minValue,
494 maxDate: me.maxValue,
495 disabledDatesRE: me.disabledDatesRE,
496 disabledDatesText: me.disabledDatesText,
497 disabledDays: me.disabledDays,
498 disabledDaysText: me.disabledDaysText,
500 showToday: me.showToday,
501 startDay: me.startDay,
502 minText: format(me.minText, me.formatDate(me.minValue)),
503 maxText: format(me.maxText, me.formatDate(me.maxValue)),
516 onSelect: function(m, d) {
520 me.fireEvent('select', me, d);
524 <span id='Ext-form-field-Date-method-onExpand'> /**
526 * Sets the Date picker's value to match the current field value when expanding.
528 onExpand: function() {
530 value = me.getValue();
531 me.picker.setValue(Ext.isDate(value) ? value : new Date());
534 <span id='Ext-form-field-Date-method-onCollapse'> /**
536 * Focuses the field when collapsing the Date picker.
538 onCollapse: function() {
539 this.focus(false, 60);
543 beforeBlur : function(){
545 v = me.parseDate(me.getRawValue()),
546 focusTask = me.focusTask;
557 <span id='Ext-form-field-Date-cfg-grow'> /**
558 </span> * @cfg {Boolean} grow @hide
560 <span id='Ext-form-field-Date-cfg-growMin'> /**
561 </span> * @cfg {Number} growMin @hide
563 <span id='Ext-form-field-Date-cfg-growMax'> /**
564 </span> * @cfg {Number} growMax @hide
566 <span id='Ext-form-field-Date-method-autoSize'> /**