如何使用 min/maxValue 验证 Ext.form.DateField,而不使用时间?

发布于 2024-10-02 22:29:05 字数 1563 浏览 5 评论 0原文

我遇到以下问题:我想验证 DateField 以便它的值介于 minValue / maxValue 范围之间。 (大于或等于,小于或等于)

问题是我认为框架花费的时间以毫秒为单位。

我尝试过使用自定义 vtype,例如:

Ext.apply(Ext.form.VTypes,{
dates: function(val, field){
    try{
        if(this.disabled){
            return true;
        }

        if(Ext.value(val,null,false)==null){
            this.datesText = "This field is required.";
            return this.allowBlank; //the text message won't be shown in case blank is allowed.
        }

        if(Ext.value(field.minValue,null,false)!=null){
            if(Ext.util.Format.date(val,"Ymd")<Ext.util.Format.date(field.minValue,"Ymd")){
                this.datesText = "The value is invalid.<br/>";
                this.datesText = "it must be greater or equal than " + field.minValue;
                return false;
            }
        }

        if(Ext.value(field.maxValue,null,false)!=null){
            if(Ext.util.Format.date(val,"Ymd")>Ext.util.Format.date(field.maxValue,"Ymd")){
                this.datesText = "The value is invalid.<br/>";
                this.datesText = "It must be lower or equal than " + field.maxValue;
                return false;
            }
        }

        return true;

    }catch(e){
        return false;
    }
},
datesText: 'The value is invalid.', //error message
datesMask: / /  //regexp to filter the characters allowed

});

基本上,它的作用是将值转换为“Ymd”格式,然后将值作为数字进行比较。

如果我调试此代码,验证会正常进行,但由于某种原因我仍然收到错误消息。我相信框架正在尝试在我验证后再次验证该字段。

谢谢你!

塞巴斯蒂安

I'm having the following problem: I want to validate a DateField so that it has a value between the minValue / maxValue range. (greater or equal, lower or equal)

The problem is that I think the framework takes the time in milliseconds.

I've tried using a custom vtype such as:

Ext.apply(Ext.form.VTypes,{
dates: function(val, field){
    try{
        if(this.disabled){
            return true;
        }

        if(Ext.value(val,null,false)==null){
            this.datesText = "This field is required.";
            return this.allowBlank; //the text message won't be shown in case blank is allowed.
        }

        if(Ext.value(field.minValue,null,false)!=null){
            if(Ext.util.Format.date(val,"Ymd")<Ext.util.Format.date(field.minValue,"Ymd")){
                this.datesText = "The value is invalid.<br/>";
                this.datesText = "it must be greater or equal than " + field.minValue;
                return false;
            }
        }

        if(Ext.value(field.maxValue,null,false)!=null){
            if(Ext.util.Format.date(val,"Ymd")>Ext.util.Format.date(field.maxValue,"Ymd")){
                this.datesText = "The value is invalid.<br/>";
                this.datesText = "It must be lower or equal than " + field.maxValue;
                return false;
            }
        }

        return true;

    }catch(e){
        return false;
    }
},
datesText: 'The value is invalid.', //error message
datesMask: / /  //regexp to filter the characters allowed

});

Basically what it does is convert the values to a 'Ymd' format and then compare values as numbers.

If I debug this code, the validation goes okay, but for some reason I still get an error message. I believe the framework is trying to validate the field again after my validation.

Thank you!

Sebastián

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难如初 2024-10-09 22:29:05
minValue : Date/String

允许的最短日期。可以是 Javascript 日期对象或有效格式的字符串日期(默认为 null)。

maxValue : Date/String

允许的最大日期。可以是 Javascript 日期对象或有效格式的字符串日期(默认为 null)。

如果您需要禁用某些日期,

disabledDates : Array

则要禁用的“日期”数组(作为字符串)。这些字符串将用于构建动态正则表达式,因此它们非常强大。一些例子:
// 禁用这些确切日期:

disabledDates: ["03/08/2003", "09/16/2003"]

// 禁用每年的这些天:

disabledDates: ["03/08", "09/16"]

// 只匹配开头(如果您使用短年份则有用):

disabledDates: ["^03/08"]

// 禁用 2006 年 3 月的每一天:

disabledDates: ["03/../2006"]

// 禁用每年 3 月的每一天:

disabledDates: ["^03"]
minValue : Date/String

The minimum allowed date. Can be either a Javascript date object or a string date in a valid format (defaults to null).

maxValue : Date/String

The maximum allowed date. Can be either a Javascript date object or a string date in a valid format (defaults to null).

and in case you needed to disable some dates

disabledDates : Array

An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular expression so they are very powerful. Some examples:
// disable these exact dates:

disabledDates: ["03/08/2003", "09/16/2003"]

// disable these days for every year:

disabledDates: ["03/08", "09/16"]

// only match the beginning (useful if you are using short years):

disabledDates: ["^03/08"]

// disable every day in March 2006:

disabledDates: ["03/../2006"]

// disable every day in every March:

disabledDates: ["^03"]
卸妝后依然美 2024-10-09 22:29:05

使用以下命令代替上面提到的固定日期:

//doesn't allow past today
maxValue: new Date() 
//Only allows 7 days in the past from current date.
minValue: Ext.Date.add(new Date(), Ext.Date.DAY, -7) 

Instead of fixed dates mentioned above, use this:

//doesn't allow past today
maxValue: new Date() 
//Only allows 7 days in the past from current date.
minValue: Ext.Date.add(new Date(), Ext.Date.DAY, -7) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文