Pergunta

I am using ExtJS 2.1 and I have the following problem, I hate a 'datefield'. Now the date has to be entered in the format 'MM/DD/YYYY'. The problem is if the user enters something like '21/17' or '16/05' it gets converted to a valid date. (21/17 gets converted to 9/17/2015 and 16/05 gets converted to 4/05/2015). How do I override this behavior? I tried writing my own validator but that didn't help either, even if my validator returns 'false' the conversion still happens. Here is the code below:

var d = new Ext.form.DateField({
            el: el.dom,
            id: id,
            format: 'm/d/Y',
            hideTrigger: false,
            allowBlank: true,
            disabled: isDisabled,
            validator: testForShortDate,
            validateOnBlur: true,
            minLength:6,
            //validationEvent: false,  //string or boolean
            invalidText: 'Enter date as MM/DD/YYYY',
            menuListeners: Ext.applyIf({
                select: function (m, d) {
                    Ext.form.DateField.prototype.menuListeners.select.apply(this, arguments);
                    this.focus.defer(100, this);
                    onDateSelect(m, d, this);
                }
            })
        });

        d.render();

d

function testForShortDate(date) {
if (date.split("/").length != 3) {
    console.log(date.split("/").length);
    return false;
}
return true;

Can anyone help?

Foi útil?

Solução

There are alternative date formats which ExtJS will try to use if the datefield's value cannot be parsed using the configured format. These formats can be defined using the altFormats property.

By default the value is:

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

which explains why something like 21/17 gets converted to 9/17/2015, as the format m/d is used here (the "21st" month of 2014 is really the 9th of 2015).

If you want to disable this altogether, just set the property to an empty string:

altFormats: ''
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top