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.MonthField
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 MonthField
18 * @param {Object} config
20 Roo.form.MonthField = function(config){
22 Roo.form.MonthField.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") this.minValue = this.parseDate(this.minValue);
38 if(typeof this.maxValue == "string") this.maxValue = this.parseDate(this.maxValue);
40 if(this.disabledDates){
41 var dd = this.disabledDates;
43 for(var i = 0; i < dd.length; i++){
45 if(i != dd.length-1) re += "|";
47 this.ddMatch = new RegExp(re + ")");
51 Roo.extend(Roo.form.MonthField, Roo.form.TriggerField, {
53 * @cfg {String} format
54 * The default date format string which can be overriden for localization support. The format must be
55 * valid according to {@link Date#parseDate} (defaults to 'm/d/y').
59 * @cfg {String} altFormats
60 * Multiple date formats separated by "|" to try when parsing a user input value and it doesn't match the defined
61 * format (defaults to 'm/d/Y|m-d-y|m-d-Y|m/d|m-d|d').
63 altFormats : "m/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d",
65 * @cfg {Array} disabledDays
66 * An array of days to disable, 0 based. For example, [0, 6] disables Sunday and Saturday (defaults to null).
68 disabledDays : [0,1,2,3,4,5,6],
70 * @cfg {String} disabledDaysText
71 * The tooltip to display when the date falls on a disabled day (defaults to 'Disabled')
73 disabledDaysText : "Disabled",
75 * @cfg {Array} disabledDates
76 * An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
77 * expression so they are very powerful. Some examples:
79 * <li>["03/08/2003", "09/16/2003"] would disable those exact dates</li>
80 * <li>["03/08", "09/16"] would disable those days for every year</li>
81 * <li>["^03/08"] would only match the beginning (useful if you are using short years)</li>
82 * <li>["03/../2006"] would disable every day in March 2006</li>
83 * <li>["^03"] would disable every day in every March</li>
85 * In order to support regular expressions, if you are using a date format that has "." in it, you will have to
86 * escape the dot when restricting dates. For example: ["03\\.08\\.03"].
90 * @cfg {String} disabledDatesText
91 * The tooltip text to display when the date falls on a disabled date (defaults to 'Disabled')
93 disabledDatesText : "Disabled",
95 * @cfg {Date/String} minValue
96 * The minimum allowed date. Can be either a Javascript date object or a string date in a
97 * valid format (defaults to null).
101 * @cfg {Date/String} maxValue
102 * The maximum allowed date. Can be either a Javascript date object or a string date in a
103 * valid format (defaults to null).
107 * @cfg {String} minText
108 * The error text to display when the date in the cell is before minValue (defaults to
109 * 'The date in this field must be after {minValue}').
111 minText : "The date in this field must be equal to or after {0}",
113 * @cfg {String} maxText
114 * The error text to display when the date in the cell is after maxValue (defaults to
115 * 'The date in this field must be before {maxValue}').
117 maxText : "The date in this field must be equal to or before {0}",
119 * @cfg {String} invalidText
120 * The error text to display when the date in the field is invalid (defaults to
121 * '{value} is not a valid date - it must be in the format {format}').
123 invalidText : "{0} is not a valid date - it must be in the format {1}",
125 * @cfg {String} triggerClass
126 * An additional CSS class used to style the trigger button. The trigger will always get the
127 * class 'x-form-trigger' and triggerClass will be <b>appended</b> if specified (defaults to 'x-form-date-trigger'
128 * which displays a calendar icon).
130 triggerClass : 'x-form-date-trigger',
134 * @cfg {Boolean} useIso
135 * if enabled, then the date field will use a hidden field to store the
136 * real value as iso formated date. default (false)
140 * @cfg {String/Object} autoCreate
141 * A DomHelper element spec, or true for a default element spec (defaults to
142 * {tag: "input", type: "text", size: "10", autocomplete: "off"})
145 defaultAutoCreate : {tag: "input", type: "text", size: "10", autocomplete: "off"},
150 hideMonthPicker : false,
152 onRender : function(ct, position)
154 Roo.form.MonthField.superclass.onRender.call(this, ct, position);
156 this.el.dom.removeAttribute('name');
157 this.hiddenField = this.el.insertSibling({ tag:'input', type:'hidden', name: this.name },
159 this.hiddenField.value = this.value ? this.formatDate(this.value, 'Y-m-d') : '';
160 // prevent input submission
161 this.hiddenName = this.name;
168 validateValue : function(value)
170 value = this.formatDate(value);
171 if(!Roo.form.DateField.superclass.validateValue.call(this, value)){
174 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
178 value = this.parseDate(value);
180 this.markInvalid(String.format(this.invalidText, svalue, this.format));
183 var time = value.getTime();
184 if(this.minValue && time < this.minValue.getTime()){
185 this.markInvalid(String.format(this.minText, this.formatDate(this.minValue)));
188 if(this.maxValue && time > this.maxValue.getTime()){
189 this.markInvalid(String.format(this.maxText, this.formatDate(this.maxValue)));
192 if(this.disabledDays){
193 var day = value.getDay();
194 for(var i = 0; i < this.disabledDays.length; i++) {
195 if(day === this.disabledDays[i]){
196 this.markInvalid(this.disabledDaysText);
201 var fvalue = this.formatDate(value);
202 if(this.ddMatch && this.ddMatch.test(fvalue)){
203 this.markInvalid(String.format(this.disabledDatesText, fvalue));
210 // Provides logic to override the default TriggerField.validateBlur which just returns true
211 validateBlur : function(){
212 return !this.menu || !this.menu.isVisible();
216 * Returns the current date value of the date field.
217 * @return {Date} The date value
219 getValue : function(){
221 return this.hiddenField ?
222 this.hiddenField.value :
223 this.parseDate(Roo.form.DateField.superclass.getValue.call(this)) || "";
227 * Sets the value of the date field. You can pass a date object or any string that can be parsed into a valid
228 * date, using DateField.format as the date format, according to the same rules as {@link Date#parseDate}
229 * (the default format used is "m/d/y").
232 //All of these calls set the same date value (May 4, 2006)
234 //Pass a date object:
235 var dt = new Date('5/4/06');
236 dateField.setValue(dt);
238 //Pass a date string (default format):
239 dateField.setValue('5/4/06');
241 //Pass a date string (custom format):
242 dateField.format = 'Y-m-d';
243 dateField.setValue('2006-5-4');
245 * @param {String/Date} date The date or valid date string
247 setValue : function(date){
248 if (this.hiddenField) {
249 this.hiddenField.value = this.formatDate(this.parseDate(date), 'Y-m-d');
251 Roo.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
255 parseDate : function(value){
256 if(!value || value instanceof Date){
259 var v = Date.parseDate(value, this.format);
260 if(!v && this.altFormats){
261 if(!this.altFormatsArray){
262 this.altFormatsArray = this.altFormats.split("|");
264 for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
265 v = Date.parseDate(value, this.altFormatsArray[i]);
272 formatDate : function(date, fmt){
273 return (!date || !(date instanceof Date)) ?
274 date : date.dateFormat(fmt || this.format);
279 select: function(m, d){
281 this.fireEvent('select', this, d);
283 show : function(){ // retain focus styling
287 this.focus.defer(10, this);
288 var ml = this.menuListeners;
289 this.menu.un("select", ml.select, this);
290 this.menu.un("show", ml.show, this);
291 this.menu.un("hide", ml.hide, this);
295 // Implements the default empty TriggerField.onTriggerClick function to display the DatePicker
296 onTriggerClick : function(){
300 if(this.menu == null){
301 this.menu = new Roo.menu.DateMenu();
304 Roo.apply(this.menu.picker, {
306 showClear: this.allowBlank,
307 minDate : this.minValue,
308 maxDate : this.maxValue,
309 disabledDatesRE : this.ddMatch,
310 disabledDatesText : this.disabledDatesText,
312 format : this.format,
313 minText : String.format(this.minText, this.formatDate(this.minValue)),
314 maxText : String.format(this.maxText, this.formatDate(this.maxValue))
318 this.menu.on(Roo.apply({}, this.menuListeners, {
322 this.menu.picker.setValue(this.getValue() || new Date());
323 this.menu.show(this.el, "tl-bl?");
324 var p = this.menu.picker;
327 Roo.log("show month");
333 p.hideMonthPicker = function(disableAnim){
334 if(this.monthPicker){
335 if(disableAnim === true){
336 this.monthPicker.hide();
338 this.monthPicker.slideOut('t', {duration:.2});
341 this.mpSelMonth = (this.activeDate || this.value).getMonth();
342 this.updateMPMonth(this.mpSelMonth);
343 this.mpSelYear = (this.activeDate || this.value).getFullYear();
344 this.updateMPYear(this.mpSelYear);
345 Roo.log(m.picker.mpSelMonth);
346 Roo.log(m.picker.mpSelYear);
355 beforeBlur : function(){
356 var v = this.parseDate(this.getRawValue());
362 /** @cfg {Boolean} grow @hide */
363 /** @cfg {Number} growMin @hide */
364 /** @cfg {Number} growMax @hide */