4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.form.DateField
14 * @extends Roo.form.TriggerField
15 * Provides a date input field with a {@link Roo.DatePicker} dropdown and automatic date validation.
17 * Create a new DateField
18 * @param {Object} config
20 Roo.form.DateField = function(config){
21 Roo.form.DateField.superclass.constructor.call(this, config);
27 * Fires when a date is selected
28 * @param {Roo.form.DateField} combo This combo box
29 * @param {Date} date The date selected
36 if(typeof this.minValue == "string") this.minValue = this.parseDate(this.minValue);
37 if(typeof this.maxValue == "string") this.maxValue = this.parseDate(this.maxValue);
39 if(this.disabledDates){
40 var dd = this.disabledDates;
42 for(var i = 0; i < dd.length; i++){
44 if(i != dd.length-1) re += "|";
46 this.ddMatch = new RegExp(re + ")");
50 Roo.extend(Roo.form.DateField, Roo.form.TriggerField, {
52 * @cfg {String} format
53 * The default date format string which can be overriden for localization support. The format must be
54 * valid according to {@link Date#parseDate} (defaults to 'm/d/y').
58 * @cfg {String} altFormats
59 * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
60 * format (defaults to 'm/d/Y|m-d-y|m-d-Y|m/d|m-d|d').
62 altFormats : "m/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d",
64 * @cfg {Array} disabledDays
65 * An array of days to disable, 0 based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
69 * @cfg {String} disabledDaysText
70 * The tooltip to display when the date falls on a disabled day (defaults to 'Disabled')
72 disabledDaysText : "Disabled",
74 * @cfg {Array} disabledDates
75 * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
76 * expression so they are very powerful. Some examples:
78 * <li>["03/08/2003", "09/16/2003"] would disable those exact dates</li>
79 * <li>["03/08", "09/16"] would disable those days for every year</li>
80 * <li>["^03/08"] would only match the beginning (useful if you are using short years)</li>
81 * <li>["03/../2006"] would disable every day in March 2006</li>
82 * <li>["^03"] would disable every day in every March</li>
84 * In order to support regular expressions, if you are using a date format that has "." in it, you will have to
85 * escape the dot when restricting dates. For example: ["03\\.08\\.03"].
89 * @cfg {String} disabledDatesText
90 * The tooltip text to display when the date falls on a disabled date (defaults to 'Disabled')
92 disabledDatesText : "Disabled",
94 * @cfg {Date/String} minValue
95 * The minimum allowed date. Can be either a Javascript date object or a string date in a
96 * valid format (defaults to null).
100 * @cfg {Date/String} maxValue
101 * The maximum allowed date. Can be either a Javascript date object or a string date in a
102 * valid format (defaults to null).
106 * @cfg {String} minText
107 * The error text to display when the date in the cell is before minValue (defaults to
108 * 'The date in this field must be after {minValue}').
110 minText : "The date in this field must be equal to or after {0}",
112 * @cfg {String} maxText
113 * The error text to display when the date in the cell is after maxValue (defaults to
114 * 'The date in this field must be before {maxValue}').
116 maxText : "The date in this field must be equal to or before {0}",
118 * @cfg {String} invalidText
119 * The error text to display when the date in the field is invalid (defaults to
120 * '{value} is not a valid date - it must be in the format {format}').
122 invalidText : "{0} is not a valid date - it must be in the format {1}",
124 * @cfg {String} triggerClass
125 * An additional CSS class used to style the trigger button. The trigger will always get the
126 * class 'x-form-trigger' and triggerClass will be <b>appended</b> if specified (defaults to 'x-form-date-trigger'
127 * which displays a calendar icon).
129 triggerClass : 'x-form-date-trigger',
134 * if enabled, then the date field will use a hidden field to store the
135 * real value as iso formated date. default (false)
139 * @cfg {String/Object} autoCreate
140 * A DomHelper element spec, or true for a default element spec (defaults to
141 * {tag: "input", type: "text", size: "10", autocomplete: "off"})
144 defaultAutoCreate : {tag: "input", type: "text", size: "10", autocomplete: "off"},
149 onRender : function(ct, position)
151 Roo.form.DateField.superclass.onRender.call(this, ct, position);
153 this.el.dom.removeAttribute('name');
154 this.hiddenField = this.el.insertSibling({ tag:'input', type:'hidden', name: this.name },
156 this.hiddenField.value = this.formatDate(this.value, 'Y-m-d');
157 // prevent input submission
158 this.hiddenName = this.name;
165 validateValue : function(value)
167 value = this.formatDate(value);
168 if(!Roo.form.DateField.superclass.validateValue.call(this, value)){
171 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
175 value = this.parseDate(value);
177 this.markInvalid(String.format(this.invalidText, svalue, this.format));
180 var time = value.getTime();
181 if(this.minValue && time < this.minValue.getTime()){
182 this.markInvalid(String.format(this.minText, this.formatDate(this.minValue)));
185 if(this.maxValue && time > this.maxValue.getTime()){
186 this.markInvalid(String.format(this.maxText, this.formatDate(this.maxValue)));
189 if(this.disabledDays){
190 var day = value.getDay();
191 for(var i = 0; i < this.disabledDays.length; i++) {
192 if(day === this.disabledDays[i]){
193 this.markInvalid(this.disabledDaysText);
198 var fvalue = this.formatDate(value);
199 if(this.ddMatch && this.ddMatch.test(fvalue)){
200 this.markInvalid(String.format(this.disabledDatesText, fvalue));
207 // Provides logic to override the default TriggerField.validateBlur which just returns true
208 validateBlur : function(){
209 return !this.menu || !this.menu.isVisible();
213 * Returns the current date value of the date field.
214 * @return {Date} The date value
216 getValue : function(){
218 return this.hiddenField ? this.hiddenField.value : this.parseDate(Roo.form.DateField.superclass.getValue.call(this)) || "";
222 * Sets the value of the date field. You can pass a date object or any string that can be parsed into a valid
223 * date, using DateField.format as the date format, according to the same rules as {@link Date#parseDate}
224 * (the default format used is "m/d/y").
227 //All of these calls set the same date value (May 4, 2006)
229 //Pass a date object:
230 var dt = new Date('5/4/06');
231 dateField.setValue(dt);
233 //Pass a date string (default format):
234 dateField.setValue('5/4/06');
236 //Pass a date string (custom format):
237 dateField.format = 'Y-m-d';
238 dateField.setValue('2006-5-4');
240 * @param {String/Date} date The date or valid date string
242 setValue : function(date){
243 if (this.hiddenField) {
244 this.hiddenField.value = this.formatDate(this.parseDate(date), 'Y-m-d');
246 Roo.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
250 parseDate : function(value){
251 if(!value || value instanceof Date){
254 var v = Date.parseDate(value, this.format);
255 if(!v && this.altFormats){
256 if(!this.altFormatsArray){
257 this.altFormatsArray = this.altFormats.split("|");
259 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
260 v = Date.parseDate(value, this.altFormatsArray[i]);
267 formatDate : function(date, fmt){
268 return (!date || !(date instanceof Date)) ?
269 date : date.dateFormat(fmt || this.format);
274 select: function(m, d){
276 this.fireEvent('select', this, d);
278 show : function(){ // retain focus styling
282 this.focus.defer(10, this);
283 var ml = this.menuListeners;
284 this.menu.un("select", ml.select, this);
285 this.menu.un("show", ml.show, this);
286 this.menu.un("hide", ml.hide, this);
291 // Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
292 onTriggerClick : function(){
296 if(this.menu == null){
297 this.menu = new Roo.menu.DateMenu();
299 Roo.apply(this.menu.picker, {
300 showClear: this.allowBlank,
301 minDate : this.minValue,
302 maxDate : this.maxValue,
303 disabledDatesRE : this.ddMatch,
304 disabledDatesText : this.disabledDatesText,
305 disabledDays : this.disabledDays,
306 disabledDaysText : this.disabledDaysText,
307 format : this.format,
308 minText : String.format(this.minText, this.formatDate(this.minValue)),
309 maxText : String.format(this.maxText, this.formatDate(this.maxValue))
311 this.menu.on(Roo.apply({}, this.menuListeners, {
314 this.menu.picker.setValue(this.getValue() || new Date());
315 this.menu.show(this.el, "tl-bl?");
318 beforeBlur : function(){
319 var v = this.parseDate(this.getRawValue());
325 /** @cfg {Boolean} grow @hide */
326 /** @cfg {Number} growMin @hide */
327 /** @cfg {Number} growMax @hide */