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)
22 Roo.form.DateField.superclass.constructor.call(this, config);
28 * Fires when a date is selected
29 * @param {Roo.form.DateField} combo This combo box
30 * @param {Date} date The date selected
37 if(typeof this.minValue == "string") {
38 this.minValue = this.parseDate(this.minValue);
40 if(typeof this.maxValue == "string") {
41 this.maxValue = this.parseDate(this.maxValue);
44 if(this.disabledDates){
45 var dd = this.disabledDates;
47 for(var i = 0; i < dd.length; i++){
49 if(i != dd.length-1) {
53 this.ddMatch = new RegExp(re + ")");
57 Roo.extend(Roo.form.DateField, Roo.form.TriggerField, {
59 * @cfg {String} format
60 * The default date format string which can be overriden for localization support. The format must be
61 * valid according to {@link Date#parseDate} (defaults to 'm/d/y').
65 * @cfg {String} altFormats
66 * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
67 * format (defaults to 'm/d/Y|m-d-y|m-d-Y|m/d|m-d|d').
69 altFormats : "m/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d",
71 * @cfg {Array} disabledDays
72 * An array of days to disable, 0 based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
76 * @cfg {String} disabledDaysText
77 * The tooltip to display when the date falls on a disabled day (defaults to 'Disabled')
79 disabledDaysText : "Disabled",
81 * @cfg {Array} disabledDates
82 * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
83 * expression so they are very powerful. Some examples:
85 * <li>["03/08/2003", "09/16/2003"] would disable those exact dates</li>
86 * <li>["03/08", "09/16"] would disable those days for every year</li>
87 * <li>["^03/08"] would only match the beginning (useful if you are using short years)</li>
88 * <li>["03/../2006"] would disable every day in March 2006</li>
89 * <li>["^03"] would disable every day in every March</li>
91 * In order to support regular expressions, if you are using a date format that has "." in it, you will have to
92 * escape the dot when restricting dates. For example: ["03\\.08\\.03"].
96 * @cfg {String} disabledDatesText
97 * The tooltip text to display when the date falls on a disabled date (defaults to 'Disabled')
99 disabledDatesText : "Disabled",
101 * @cfg {Date/String} minValue
102 * The minimum allowed date. Can be either a Javascript date object or a string date in a
103 * valid format (defaults to null).
107 * @cfg {Date/String} maxValue
108 * The maximum allowed date. Can be either a Javascript date object or a string date in a
109 * valid format (defaults to null).
113 * @cfg {String} minText
114 * The error text to display when the date in the cell is before minValue (defaults to
115 * 'The date in this field must be after {minValue}').
117 minText : "The date in this field must be equal to or after {0}",
119 * @cfg {String} maxText
120 * The error text to display when the date in the cell is after maxValue (defaults to
121 * 'The date in this field must be before {maxValue}').
123 maxText : "The date in this field must be equal to or before {0}",
125 * @cfg {String} invalidText
126 * The error text to display when the date in the field is invalid (defaults to
127 * '{value} is not a valid date - it must be in the format {format}').
129 invalidText : "{0} is not a valid date - it must be in the format {1}",
131 * @cfg {String} triggerClass
132 * An additional CSS class used to style the trigger button. The trigger will always get the
133 * class 'x-form-trigger' and triggerClass will be <b>appended</b> if specified (defaults to 'x-form-date-trigger'
134 * which displays a calendar icon).
136 triggerClass : 'x-form-date-trigger',
140 * @cfg {Boolean} useIso
141 * if enabled, then the date field will use a hidden field to store the
142 * real value as iso formated date. default (false)
146 * @cfg {String/Object} autoCreate
147 * A DomHelper element spec, or true for a default element spec (defaults to
148 * {tag: "input", type: "text", size: "10", autocomplete: "off"})
151 defaultAutoCreate : {tag: "input", type: "text", size: "10", autocomplete: "off"},
156 onRender : function(ct, position)
158 Roo.form.DateField.superclass.onRender.call(this, ct, position);
160 //this.el.dom.removeAttribute('name');
161 Roo.log("Changing name?");
162 this.el.dom.setAttribute('name', this.name + '____hidden___' );
163 this.hiddenField = this.el.insertSibling({ tag:'input', type:'hidden', name: this.name },
165 this.hiddenField.value = this.value ? this.formatDate(this.value, 'Y-m-d') : '';
166 // prevent input submission
167 this.hiddenName = this.name;
174 validateValue : function(value)
176 value = this.formatDate(value);
177 if(!Roo.form.DateField.superclass.validateValue.call(this, value)){
178 Roo.log('super failed');
181 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
185 value = this.parseDate(value);
187 Roo.log('parse date failed' + svalue);
188 this.markInvalid(String.format(this.invalidText, svalue, this.format));
191 var time = value.getTime();
192 if(this.minValue && time < this.minValue.getTime()){
193 this.markInvalid(String.format(this.minText, this.formatDate(this.minValue)));
196 if(this.maxValue && time > this.maxValue.getTime()){
197 this.markInvalid(String.format(this.maxText, this.formatDate(this.maxValue)));
200 if(this.disabledDays){
201 var day = value.getDay();
202 for(var i = 0; i < this.disabledDays.length; i++) {
203 if(day === this.disabledDays[i]){
204 this.markInvalid(this.disabledDaysText);
209 var fvalue = this.formatDate(value);
210 if(this.ddMatch && this.ddMatch.test(fvalue)){
211 this.markInvalid(String.format(this.disabledDatesText, fvalue));
218 // Provides logic to override the default TriggerField.validateBlur which just returns true
219 validateBlur : function(){
220 return !this.menu || !this.menu.isVisible();
225 // returns hidden if it's set..
226 if (!this.rendered) {return ''};
227 return !this.hiddenName && this.el.dom.name ? this.el.dom.name : (this.hiddenName || '');
232 * Returns the current date value of the date field.
233 * @return {Date} The date value
235 getValue : function(){
237 return this.hiddenField ?
238 this.hiddenField.value :
239 this.parseDate(Roo.form.DateField.superclass.getValue.call(this)) || "";
243 * Sets the value of the date field. You can pass a date object or any string that can be parsed into a valid
244 * date, using DateField.format as the date format, according to the same rules as {@link Date#parseDate}
245 * (the default format used is "m/d/y").
248 //All of these calls set the same date value (May 4, 2006)
250 //Pass a date object:
251 var dt = new Date('5/4/06');
252 dateField.setValue(dt);
254 //Pass a date string (default format):
255 dateField.setValue('5/4/06');
257 //Pass a date string (custom format):
258 dateField.format = 'Y-m-d';
259 dateField.setValue('2006-5-4');
261 * @param {String/Date} date The date or valid date string
263 setValue : function(date){
264 if (this.hiddenField) {
265 this.hiddenField.value = this.formatDate(this.parseDate(date), 'Y-m-d');
267 Roo.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
268 // make sure the value field is always stored as a date..
269 this.value = this.parseDate(date);
275 parseDate : function(value){
276 if(!value || value instanceof Date){
279 var v = Date.parseDate(value, this.format);
280 if (!v && this.useIso) {
281 v = Date.parseDate(value, 'Y-m-d');
283 if(!v && this.altFormats){
284 if(!this.altFormatsArray){
285 this.altFormatsArray = this.altFormats.split("|");
287 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
288 v = Date.parseDate(value, this.altFormatsArray[i]);
295 formatDate : function(date, fmt){
296 return (!date || !(date instanceof Date)) ?
297 date : date.dateFormat(fmt || this.format);
302 select: function(m, d){
305 this.fireEvent('select', this, d);
307 show : function(){ // retain focus styling
311 this.focus.defer(10, this);
312 var ml = this.menuListeners;
313 this.menu.un("select", ml.select, this);
314 this.menu.un("show", ml.show, this);
315 this.menu.un("hide", ml.hide, this);
320 // Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
321 onTriggerClick : function(){
325 if(this.menu == null){
326 this.menu = new Roo.menu.DateMenu();
328 Roo.apply(this.menu.picker, {
329 showClear: this.allowBlank,
330 minDate : this.minValue,
331 maxDate : this.maxValue,
332 disabledDatesRE : this.ddMatch,
333 disabledDatesText : this.disabledDatesText,
334 disabledDays : this.disabledDays,
335 disabledDaysText : this.disabledDaysText,
336 format : this.useIso ? 'Y-m-d' : this.format,
337 minText : String.format(this.minText, this.formatDate(this.minValue)),
338 maxText : String.format(this.maxText, this.formatDate(this.maxValue))
340 this.menu.on(Roo.apply({}, this.menuListeners, {
343 this.menu.picker.setValue(this.getValue() || new Date());
344 this.menu.show(this.el, "tl-bl?");
347 beforeBlur : function(){
348 var v = this.parseDate(this.getRawValue());
358 isDirty : function() {
363 if(typeof(this.startValue) === 'undefined'){
367 return String(this.getValue()) !== String(this.startValue);
371 cleanLeadingSpace : function(e)