3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.form.DateField"></div>/**
10 * @class Ext.form.DateField
11 * @extends Ext.form.TriggerField
12 * Provides a date input field with a {@link Ext.DatePicker} dropdown and automatic date validation.
14 * Create a new DateField
15 * @param {Object} config
18 Ext.form.DateField = Ext.extend(Ext.form.TriggerField, {
19 <div id="cfg-Ext.form.DateField-format"></div>/**
20 * @cfg {String} format
21 * The default date format string which can be overriden for localization support. The format must be
22 * valid according to {@link Date#parseDate} (defaults to <tt>'m/d/Y'</tt>).
25 <div id="cfg-Ext.form.DateField-altFormats"></div>/**
26 * @cfg {String} altFormats
27 * Multiple date formats separated by "<tt>|</tt>" to try when parsing a user input value and it
28 * does not match the defined format (defaults to
29 * <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'</tt>).
31 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",
32 <div id="cfg-Ext.form.DateField-disabledDaysText"></div>/**
33 * @cfg {String} disabledDaysText
34 * The tooltip to display when the date falls on a disabled day (defaults to <tt>'Disabled'</tt>)
36 disabledDaysText : "Disabled",
37 <div id="cfg-Ext.form.DateField-disabledDatesText"></div>/**
38 * @cfg {String} disabledDatesText
39 * The tooltip text to display when the date falls on a disabled date (defaults to <tt>'Disabled'</tt>)
41 disabledDatesText : "Disabled",
42 <div id="cfg-Ext.form.DateField-minText"></div>/**
43 * @cfg {String} minText
44 * The error text to display when the date in the cell is before <tt>{@link #minValue}</tt> (defaults to
45 * <tt>'The date in this field must be after {minValue}'</tt>).
47 minText : "The date in this field must be equal to or after {0}",
48 <div id="cfg-Ext.form.DateField-maxText"></div>/**
49 * @cfg {String} maxText
50 * The error text to display when the date in the cell is after <tt>{@link #maxValue}</tt> (defaults to
51 * <tt>'The date in this field must be before {maxValue}'</tt>).
53 maxText : "The date in this field must be equal to or before {0}",
54 <div id="cfg-Ext.form.DateField-invalidText"></div>/**
55 * @cfg {String} invalidText
56 * The error text to display when the date in the field is invalid (defaults to
57 * <tt>'{value} is not a valid date - it must be in the format {format}'</tt>).
59 invalidText : "{0} is not a valid date - it must be in the format {1}",
60 <div id="cfg-Ext.form.DateField-triggerClass"></div>/**
61 * @cfg {String} triggerClass
62 * An additional CSS class used to style the trigger button. The trigger will always get the
63 * class <tt>'x-form-trigger'</tt> and <tt>triggerClass</tt> will be <b>appended</b> if specified
64 * (defaults to <tt>'x-form-date-trigger'</tt> which displays a calendar icon).
66 triggerClass : 'x-form-date-trigger',
67 <div id="cfg-Ext.form.DateField-showToday"></div>/**
68 * @cfg {Boolean} showToday
69 * <tt>false</tt> to hide the footer area of the DatePicker containing the Today button and disable
70 * the keyboard handler for spacebar that selects the current date (defaults to <tt>true</tt>).
73 <div id="cfg-Ext.form.DateField-minValue"></div>/**
74 * @cfg {Date/String} minValue
75 * The minimum allowed date. Can be either a Javascript date object or a string date in a
76 * valid format (defaults to null).
78 <div id="cfg-Ext.form.DateField-maxValue"></div>/**
79 * @cfg {Date/String} maxValue
80 * The maximum allowed date. Can be either a Javascript date object or a string date in a
81 * valid format (defaults to null).
83 <div id="cfg-Ext.form.DateField-disabledDays"></div>/**
84 * @cfg {Array} disabledDays
85 * An array of days to disable, 0 based (defaults to null). Some examples:<pre><code>
86 // disable Sunday and Saturday:
89 disabledDays: [1,2,3,4,5]
92 <div id="cfg-Ext.form.DateField-disabledDates"></div>/**
93 * @cfg {Array} disabledDates
94 * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
95 * expression so they are very powerful. Some examples:<pre><code>
96 // disable these exact dates:
97 disabledDates: ["03/08/2003", "09/16/2003"]
98 // disable these days for every year:
99 disabledDates: ["03/08", "09/16"]
100 // only match the beginning (useful if you are using short years):
101 disabledDates: ["^03/08"]
102 // disable every day in March 2006:
103 disabledDates: ["03/../2006"]
104 // disable every day in every March:
105 disabledDates: ["^03"]
107 * Note that the format of the dates included in the array should exactly match the {@link #format} config.
108 * In order to support regular expressions, if you are using a {@link #format date format} that has "." in
109 * it, you will have to escape the dot when restricting dates. For example: <tt>["03\\.08\\.03"]</tt>.
111 <div id="cfg-Ext.form.DateField-autoCreate"></div>/**
112 * @cfg {String/Object} autoCreate
113 * A {@link Ext.DomHelper DomHelper element specification object}, or <tt>true</tt> for the default element
114 * specification object:<pre><code>
115 * autoCreate: {tag: "input", type: "text", size: "10", autocomplete: "off"}
120 defaultAutoCreate : {tag: "input", type: "text", size: "10", autocomplete: "off"},
122 initComponent : function(){
123 Ext.form.DateField.superclass.initComponent.call(this);
126 <div id="event-Ext.form.DateField-select"></div>/**
128 * Fires when a date is selected via the date picker.
129 * @param {Ext.form.DateField} this
130 * @param {Date} date The date that was selected
135 if(Ext.isString(this.minValue)){
136 this.minValue = this.parseDate(this.minValue);
138 if(Ext.isString(this.maxValue)){
139 this.maxValue = this.parseDate(this.maxValue);
141 this.disabledDatesRE = null;
142 this.initDisabledDays();
145 initEvents: function() {
146 Ext.form.DateField.superclass.initEvents.call(this);
147 this.keyNav = new Ext.KeyNav(this.el, {
148 "down": function(e) {
149 this.onTriggerClick();
158 initDisabledDays : function(){
159 if(this.disabledDates){
160 var dd = this.disabledDates,
164 Ext.each(dd, function(d, i){
165 re += Ext.isDate(d) ? '^' + Ext.escapeRe(d.dateFormat(this.format)) + '$' : dd[i];
170 this.disabledDatesRE = new RegExp(re + ')');
174 <div id="method-Ext.form.DateField-setDisabledDates"></div>/**
175 * Replaces any existing disabled dates with new values and refreshes the DatePicker.
176 * @param {Array} disabledDates An array of date strings (see the <tt>{@link #disabledDates}</tt> config
177 * for details on supported values) used to disable a pattern of dates.
179 setDisabledDates : function(dd){
180 this.disabledDates = dd;
181 this.initDisabledDays();
183 this.menu.picker.setDisabledDates(this.disabledDatesRE);
187 <div id="method-Ext.form.DateField-setDisabledDays"></div>/**
188 * Replaces any existing disabled days (by index, 0-6) with new values and refreshes the DatePicker.
189 * @param {Array} disabledDays An array of disabled day indexes. See the <tt>{@link #disabledDays}</tt>
190 * config for details on supported values.
192 setDisabledDays : function(dd){
193 this.disabledDays = dd;
195 this.menu.picker.setDisabledDays(dd);
199 <div id="method-Ext.form.DateField-setMinValue"></div>/**
200 * Replaces any existing <tt>{@link #minValue}</tt> with the new value and refreshes the DatePicker.
201 * @param {Date} value The minimum date that can be selected
203 setMinValue : function(dt){
204 this.minValue = (Ext.isString(dt) ? this.parseDate(dt) : dt);
206 this.menu.picker.setMinDate(this.minValue);
210 <div id="method-Ext.form.DateField-setMaxValue"></div>/**
211 * Replaces any existing <tt>{@link #maxValue}</tt> with the new value and refreshes the DatePicker.
212 * @param {Date} value The maximum date that can be selected
214 setMaxValue : function(dt){
215 this.maxValue = (Ext.isString(dt) ? this.parseDate(dt) : dt);
217 this.menu.picker.setMaxDate(this.maxValue);
222 validateValue : function(value){
223 value = this.formatDate(value);
224 if(!Ext.form.DateField.superclass.validateValue.call(this, value)){
227 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
231 value = this.parseDate(value);
233 this.markInvalid(String.format(this.invalidText, svalue, this.format));
236 var time = value.getTime();
237 if(this.minValue && time < this.minValue.getTime()){
238 this.markInvalid(String.format(this.minText, this.formatDate(this.minValue)));
241 if(this.maxValue && time > this.maxValue.getTime()){
242 this.markInvalid(String.format(this.maxText, this.formatDate(this.maxValue)));
245 if(this.disabledDays){
246 var day = value.getDay();
247 for(var i = 0; i < this.disabledDays.length; i++) {
248 if(day === this.disabledDays[i]){
249 this.markInvalid(this.disabledDaysText);
254 var fvalue = this.formatDate(value);
255 if(this.disabledDatesRE && this.disabledDatesRE.test(fvalue)){
256 this.markInvalid(String.format(this.disabledDatesText, fvalue));
263 // Provides logic to override the default TriggerField.validateBlur which just returns true
264 validateBlur : function(){
265 return !this.menu || !this.menu.isVisible();
268 <div id="method-Ext.form.DateField-getValue"></div>/**
269 * Returns the current date value of the date field.
270 * @return {Date} The date value
272 getValue : function(){
273 return this.parseDate(Ext.form.DateField.superclass.getValue.call(this)) || "";
276 <div id="method-Ext.form.DateField-setValue"></div>/**
277 * Sets the value of the date field. You can pass a date object or any string that can be
278 * parsed into a valid date, using <tt>{@link #format}</tt> as the date format, according
279 * to the same rules as {@link Date#parseDate} (the default format used is <tt>"m/d/Y"</tt>).
282 //All of these calls set the same date value (May 4, 2006)
284 //Pass a date object:
285 var dt = new Date('5/4/2006');
286 dateField.setValue(dt);
288 //Pass a date string (default format):
289 dateField.setValue('05/04/2006');
291 //Pass a date string (custom format):
292 dateField.format = 'Y-m-d';
293 dateField.setValue('2006-05-04');
295 * @param {String/Date} date The date or valid date string
296 * @return {Ext.form.Field} this
298 setValue : function(date){
299 return Ext.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
303 parseDate : function(value){
304 if(!value || Ext.isDate(value)){
307 var v = Date.parseDate(value, this.format);
308 if(!v && this.altFormats){
309 if(!this.altFormatsArray){
310 this.altFormatsArray = this.altFormats.split("|");
312 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
313 v = Date.parseDate(value, this.altFormatsArray[i]);
320 onDestroy : function(){
321 Ext.destroy(this.menu, this.keyNav);
322 Ext.form.DateField.superclass.onDestroy.call(this);
326 formatDate : function(date){
327 return Ext.isDate(date) ? date.dateFormat(this.format) : date;
330 <div id="method-Ext.form.DateField-onTriggerClick"></div>/**
331 * @method onTriggerClick
335 // Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
336 onTriggerClick : function(){
340 if(this.menu == null){
341 this.menu = new Ext.menu.DateMenu({
347 Ext.apply(this.menu.picker, {
348 minDate : this.minValue,
349 maxDate : this.maxValue,
350 disabledDatesRE : this.disabledDatesRE,
351 disabledDatesText : this.disabledDatesText,
352 disabledDays : this.disabledDays,
353 disabledDaysText : this.disabledDaysText,
354 format : this.format,
355 showToday : this.showToday,
356 minText : String.format(this.minText, this.formatDate(this.minValue)),
357 maxText : String.format(this.maxText, this.formatDate(this.maxValue))
359 this.menu.picker.setValue(this.getValue() || new Date());
360 this.menu.show(this.el, "tl-bl?");
361 this.menuEvents('on');
365 menuEvents: function(method){
366 this.menu[method]('select', this.onSelect, this);
367 this.menu[method]('hide', this.onMenuHide, this);
368 this.menu[method]('show', this.onFocus, this);
371 onSelect: function(m, d){
373 this.fireEvent('select', this, d);
377 onMenuHide: function(){
378 this.focus(false, 60);
379 this.menuEvents('un');
383 beforeBlur : function(){
384 var v = this.parseDate(this.getRawValue());
390 <div id="cfg-Ext.form.DateField-grow"></div>/**
391 * @cfg {Boolean} grow @hide
393 <div id="cfg-Ext.form.DateField-growMin"></div>/**
394 * @cfg {Number} growMin @hide
396 <div id="cfg-Ext.form.DateField-growMax"></div>/**
397 * @cfg {Number} growMax @hide
399 <div id="method-Ext.form.DateField-autoSize"></div>/**
404 Ext.reg('datefield', Ext.form.DateField);</pre>
\r