4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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'>/**
19 </span> * @docauthor Jason Johnston <jason@sencha.com>
21 * Provides a date input field with a {@link Ext.picker.Date date picker} dropdown and automatic date
24 * This field recognizes and uses the JavaScript Date object as its main {@link #value} type. In addition,
25 * it recognizes string values which are parsed according to the {@link #format} and/or {@link #altFormats}
26 * configs. These may be reconfigured to use date formats appropriate for the user's locale.
28 * The field may be limited to a certain range of dates by using the {@link #minValue}, {@link #maxValue},
29 * {@link #disabledDays}, and {@link #disabledDates} config parameters. These configurations will be used both
30 * in the field's validation, and in the date picker dropdown by preventing invalid dates from being selected.
35 * Ext.create('Ext.form.Panel', {
36 * renderTo: Ext.getBody(),
45 * maxValue: new Date() // limited to the current date or prior
51 * value: new Date() // defaults to today
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`.
61 * Ext.create('Ext.form.Panel', {
62 * renderTo: Ext.getBody(),
71 * // The value matches the format; will be parsed and displayed using that format.
79 * // The value does not match the format, but does match an altFormat; will be parsed
80 * // using the altFormat and displayed using the format.
82 * altFormats: 'm,d,Y|m.d.Y',
87 Ext.define('Ext.form.field.Date', {
88 extend:'Ext.form.field.Picker',
89 alias: 'widget.datefield',
90 requires: ['Ext.picker.Date'],
91 alternateClassName: ['Ext.form.DateField', 'Ext.form.Date'],
93 <span id='Ext-form-field-Date-cfg-format'> /**
94 </span> * @cfg {String} format
95 * The default date format string which can be overriden for localization support. The format must be valid
96 * according to {@link Ext.Date#parse}.
98 format : "m/d/Y",
99 <span id='Ext-form-field-Date-cfg-altFormats'> /**
100 </span> * @cfg {String} altFormats
101 * Multiple date formats separated by "|" to try when parsing a user input value and it does not match the defined
104 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",
105 <span id='Ext-form-field-Date-cfg-disabledDaysText'> /**
106 </span> * @cfg {String} disabledDaysText
107 * The tooltip to display when the date falls on a disabled day.
109 disabledDaysText : "Disabled",
110 <span id='Ext-form-field-Date-cfg-disabledDatesText'> /**
111 </span> * @cfg {String} disabledDatesText
112 * The tooltip text to display when the date falls on a disabled date.
114 disabledDatesText : "Disabled",
115 <span id='Ext-form-field-Date-cfg-minText'> /**
116 </span> * @cfg {String} minText
117 * The error text to display when the date in the cell is before {@link #minValue}.
119 minText : "The date in this field must be equal to or after {0}",
120 <span id='Ext-form-field-Date-cfg-maxText'> /**
121 </span> * @cfg {String} maxText
122 * The error text to display when the date in the cell is after {@link #maxValue}.
124 maxText : "The date in this field must be equal to or before {0}",
125 <span id='Ext-form-field-Date-cfg-invalidText'> /**
126 </span> * @cfg {String} invalidText
127 * The error text to display when the date in the field is invalid.
129 invalidText : "{0} is not a valid date - it must be in the format {1}",
130 <span id='Ext-form-field-Date-cfg-triggerCls'> /**
131 </span> * @cfg {String} [triggerCls='x-form-date-trigger']
132 * An additional CSS class used to style the trigger button. The trigger will always get the class 'x-form-trigger'
133 * and triggerCls will be **appended** if specified (default class displays a calendar icon).
135 triggerCls : Ext.baseCSSPrefix + 'form-date-trigger',
136 <span id='Ext-form-field-Date-cfg-showToday'> /**
137 </span> * @cfg {Boolean} showToday
138 * false to hide the footer area of the Date picker containing the Today button and disable the keyboard handler for
139 * spacebar that selects the current date.
142 <span id='Ext-form-field-Date-cfg-minValue'> /**
143 </span> * @cfg {Date/String} minValue
144 * The minimum allowed date. Can be either a Javascript date object or a string date in a valid format.
146 <span id='Ext-form-field-Date-cfg-maxValue'> /**
147 </span> * @cfg {Date/String} maxValue
148 * The maximum allowed date. Can be either a Javascript date object or a string date in a valid format.
150 <span id='Ext-form-field-Date-cfg-disabledDays'> /**
151 </span> * @cfg {Number[]} disabledDays
152 * An array of days to disable, 0 based. Some examples:
154 * // disable Sunday and Saturday:
155 * disabledDays: [0, 6]
156 * // disable weekdays:
157 * disabledDays: [1,2,3,4,5]
159 <span id='Ext-form-field-Date-cfg-disabledDates'> /**
160 </span> * @cfg {String[]} disabledDates
161 * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular expression so
162 * they are very powerful. Some examples:
164 * // disable these exact dates:
165 * disabledDates: ["03/08/2003", "09/16/2003"]
166 * // disable these days for every year:
167 * disabledDates: ["03/08", "09/16"]
168 * // only match the beginning (useful if you are using short years):
169 * disabledDates: ["^03/08"]
170 * // disable every day in March 2006:
171 * disabledDates: ["03/../2006"]
172 * // disable every day in every March:
173 * disabledDates: ["^03"]
175 * Note that the format of the dates included in the array should exactly match the {@link #format} config. In order
176 * to support regular expressions, if you are using a {@link #format date format} that has "." in it, you will have
177 * to escape the dot when restricting dates. For example: `["03\\.08\\.03"]`.
180 <span id='Ext-form-field-Date-cfg-submitFormat'> /**
181 </span> * @cfg {String} submitFormat
182 * The date format string which will be submitted to the server. The format must be valid according to {@link
183 * Ext.Date#parse} (defaults to {@link #format}).
186 // in the absence of a time value, a default value of 12 noon will be used
187 // (note: 12 noon was chosen because it steers well clear of all DST timezone changes)
188 initTime: '12', // 24 hour format
192 matchFieldWidth: false,
193 <span id='Ext-form-field-Date-cfg-startDay'> /**
194 </span> * @cfg {Number} startDay
195 * Day index at which the week should begin, 0-based (defaults to Sunday)
199 initComponent : function(){
201 isString = Ext.isString,
207 me.minValue = me.parseDate(min);
210 me.maxValue = me.parseDate(max);
212 me.disabledDatesRE = null;
213 me.initDisabledDays();
218 initValue: function() {
222 // If a String value was supplied, try to convert it to a proper Date
223 if (Ext.isString(value)) {
224 me.value = me.rawToValue(value);
231 initDisabledDays : function(){
232 if(this.disabledDates){
233 var dd = this.disabledDates,
235 re = "(?:";
237 Ext.each(dd, function(d, i){
238 re += Ext.isDate(d) ? '^' + Ext.String.escapeRegex(d.dateFormat(this.format)) + '$' : dd[i];
243 this.disabledDatesRE = new RegExp(re + ')');
247 <span id='Ext-form-field-Date-method-setDisabledDates'> /**
248 </span> * Replaces any existing disabled dates with new values and refreshes the Date picker.
249 * @param {String[]} disabledDates An array of date strings (see the {@link #disabledDates} config for details on
250 * supported values) used to disable a pattern of dates.
252 setDisabledDates : function(dd){
256 me.disabledDates = dd;
257 me.initDisabledDays();
259 picker.setDisabledDates(me.disabledDatesRE);
263 <span id='Ext-form-field-Date-method-setDisabledDays'> /**
264 </span> * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the Date picker.
265 * @param {Number[]} disabledDays An array of disabled day indexes. See the {@link #disabledDays} config for details on
268 setDisabledDays : function(dd){
269 var picker = this.picker;
271 this.disabledDays = dd;
273 picker.setDisabledDays(dd);
277 <span id='Ext-form-field-Date-method-setMinValue'> /**
278 </span> * Replaces any existing {@link #minValue} with the new value and refreshes the Date picker.
279 * @param {Date} value The minimum date that can be selected
281 setMinValue : function(dt){
284 minValue = (Ext.isString(dt) ? me.parseDate(dt) : dt);
286 me.minValue = minValue;
288 picker.minText = Ext.String.format(me.minText, me.formatDate(me.minValue));
289 picker.setMinDate(minValue);
293 <span id='Ext-form-field-Date-method-setMaxValue'> /**
294 </span> * Replaces any existing {@link #maxValue} with the new value and refreshes the Date picker.
295 * @param {Date} value The maximum date that can be selected
297 setMaxValue : function(dt){
300 maxValue = (Ext.isString(dt) ? me.parseDate(dt) : dt);
302 me.maxValue = maxValue;
304 picker.maxText = Ext.String.format(me.maxText, me.formatDate(me.maxValue));
305 picker.setMaxDate(maxValue);
309 <span id='Ext-form-field-Date-method-getErrors'> /**
310 </span> * Runs all of Date's validations and returns an array of any errors. Note that this first runs Text's validations,
311 * so the returned array is an amalgamation of all field errors. The additional validation checks are testing that
312 * the date format is valid, that the chosen date is within the min and max date constraints set, that the date
313 * chosen is not in the disabledDates regex and that the day chosed is not one of the disabledDays.
314 * @param {Object} [value] The value to get errors for (defaults to the current field value)
315 * @return {String[]} All validation errors for this field
317 getErrors: function(value) {
319 format = Ext.String.format,
320 clearTime = Ext.Date.clearTime,
321 errors = me.callParent(arguments),
322 disabledDays = me.disabledDays,
323 disabledDatesRE = me.disabledDatesRE,
324 minValue = me.minValue,
325 maxValue = me.maxValue,
326 len = disabledDays ? disabledDays.length : 0,
333 value = me.formatDate(value || me.processRawValue(me.getRawValue()));
335 if (value === null || value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
340 value = me.parseDate(value);
342 errors.push(format(me.invalidText, svalue, me.format));
346 time = value.getTime();
347 if (minValue && time < clearTime(minValue).getTime()) {
348 errors.push(format(me.minText, me.formatDate(minValue)));
351 if (maxValue && time > clearTime(maxValue).getTime()) {
352 errors.push(format(me.maxText, me.formatDate(maxValue)));
356 day = value.getDay();
358 for(; i < len; i++) {
359 if (day === disabledDays[i]) {
360 errors.push(me.disabledDaysText);
366 fvalue = me.formatDate(value);
367 if (disabledDatesRE && disabledDatesRE.test(fvalue)) {
368 errors.push(format(me.disabledDatesText, fvalue));
374 rawToValue: function(rawValue) {
375 return this.parseDate(rawValue) || rawValue || null;
378 valueToRaw: function(value) {
379 return this.formatDate(this.parseDate(value));
382 <span id='Ext-form-field-Date-method-setValue'> /**
383 </span> * @method setValue
384 * Sets the value of the date field. You can pass a date object or any string that can be parsed into a valid date,
385 * using {@link #format} as the date format, according to the same rules as {@link Ext.Date#parse} (the default
386 * format used is "m/d/Y").
390 * //All of these calls set the same date value (May 4, 2006)
392 * //Pass a date object:
393 * var dt = new Date('5/4/2006');
394 * dateField.setValue(dt);
396 * //Pass a date string (default format):
397 * dateField.setValue('05/04/2006');
399 * //Pass a date string (custom format):
400 * dateField.format = 'Y-m-d';
401 * dateField.setValue('2006-05-04');
403 * @param {String/Date} date The date or valid date string
404 * @return {Ext.form.field.Date} this
407 <span id='Ext-form-field-Date-method-safeParse'> /**
408 </span> * Attempts to parse a given string value using a given {@link Ext.Date#parse date format}.
409 * @param {String} value The value to attempt to parse
410 * @param {String} format A valid date format (see {@link Ext.Date#parse})
411 * @return {Date} The parsed Date object, or null if the value could not be successfully parsed.
413 safeParse : function(value, format) {
419 if (utilDate.formatContainsHourInfo(format)) {
420 // if parse format contains hour information, no DST adjustment is necessary
421 result = utilDate.parse(value, format);
423 // set time to 12 noon, then clear the time
424 parsedDate = utilDate.parse(value + ' ' + me.initTime, format + ' ' + me.initTimeFormat);
426 result = utilDate.clearTime(parsedDate);
433 getSubmitValue: function() {
434 var format = this.submitFormat || this.format,
435 value = this.getValue();
437 return value ? Ext.Date.format(value, format) : '';
440 <span id='Ext-form-field-Date-method-parseDate'> /**
443 parseDate : function(value) {
444 if(!value || Ext.isDate(value)){
449 val = me.safeParse(value, me.format),
450 altFormats = me.altFormats,
451 altFormatsArray = me.altFormatsArray,
455 if (!val && altFormats) {
456 altFormatsArray = altFormatsArray || altFormats.split('|');
457 len = altFormatsArray.length;
458 for (; i < len && !val; ++i) {
459 val = me.safeParse(value, altFormatsArray[i]);
466 formatDate : function(date){
467 return Ext.isDate(date) ? Ext.Date.dateFormat(date, this.format) : date;
470 createPicker: function() {
472 format = Ext.String.format;
474 return Ext.create('Ext.picker.Date', {
477 renderTo: document.body,
481 minDate: me.minValue,
482 maxDate: me.maxValue,
483 disabledDatesRE: me.disabledDatesRE,
484 disabledDatesText: me.disabledDatesText,
485 disabledDays: me.disabledDays,
486 disabledDaysText: me.disabledDaysText,
488 showToday: me.showToday,
489 startDay: me.startDay,
490 minText: format(me.minText, me.formatDate(me.minValue)),
491 maxText: format(me.maxText, me.formatDate(me.maxValue)),
504 onSelect: function(m, d) {
508 me.fireEvent('select', me, d);
512 <span id='Ext-form-field-Date-method-onExpand'> /**
514 * Sets the Date picker's value to match the current field value when expanding.
516 onExpand: function() {
517 var value = this.getValue();
518 this.picker.setValue(Ext.isDate(value) ? value : new Date());
521 <span id='Ext-form-field-Date-method-onCollapse'> /**
523 * Focuses the field when collapsing the Date picker.
525 onCollapse: function() {
526 this.focus(false, 60);
530 beforeBlur : function(){
532 v = me.parseDate(me.getRawValue()),
533 focusTask = me.focusTask;
544 <span id='Ext-form-field-Date-cfg-grow'> /**
546 * @cfg {Boolean} grow
548 <span id='Ext-form-field-Date-cfg-growMin'> /**
550 * @cfg {Number} growMin
552 <span id='Ext-form-field-Date-cfg-growMax'> /**
554 * @cfg {Number} growMax
556 <span id='Ext-form-field-Date-method-autoSize'> /**