ExtJS form.field.date recalculates date if non-val

2019-09-08 05:53发布

问题:

The default ExtJS form.field.date allows manual entry/editing of a date in addition to using the datepicker. However, if you enter a number larger than 31 for the DD or larger than 12 for the MM it recalculates the value.

For example, if the MM/DD/YYYY listed is 12/01/2011 and you edit it to say 15/01/2011 (where the max MM value should be 12), it recalculates to display 03/01/2012 -- assuming that you meant to add 3 beyond the max value.

Is there any way to turn off this functionality? I'd like the validation/error message to appear so the user has to manually change rather than have it automatically recalculate. This seems like basic functionality that others would want -- am I missing something in the config options?

Cross-posted in the Ext forums.

回答1:

There isn't a config option that allows you to do that.. but what you can do is provide a validator function. This function get's called before the default validation, which i assume must be changing the values' takes place.

new Ext.form.DateField({

   validator : function(value){
      var split = value.split('/'); //Assuming / is your seperator
      if(split[0] > 31)
      {
        //Show error 
      }
      // Other validations etc.
   }
});

If the value changing takes place before the validator gets called, then add config option: validateOnBlur : true



回答2:

Adding an answer to my own question found via the ext forum post.

What Ext calls the date "rollover" functionality can be turned off by adding useStrict: true. See the documentation.

I wanted to remove the rollover functionality site-wide, so I added this to our main Ext util.js that is used throughout the site:

Date.useStrict = true;

As I mentioned in the ext forum, I can't image why this rollover behavior would be useful to a front-end user -- why would someone want to intentionally enter, say, 01/33/2011 and intend to have it result in 02/02/2011.