Extjs datefields from或start end date validations

时间:2013-11-13 16:41:16

标签: validation date extjs date-range datefield

这是一个extjs mixin,用于验证fromdate todate或在表单中开始结束日期。 这里是mixin ..(核心代码取自sencha论坛:))

Ext.define("AMShiva.mixin.DateRangeValidator", {
    timerange: function (val, field) {
        /// <summary>
        /// This will validate two datefields
        /// </summary>

        var me = this; //will be the form, containing datefields

        var time = field.parseDate(val);
        if (!time) {
            return;
        }
        if (field.startTimeField && (!this.timeRangeMax || (time.getTime() != this.timeRangeMax.getTime()))) {
            var start = me.down('datefield[name=' + field.startTimeField + ']');
            start.maxValue = time;
            start.validate();
            this.timeRangeMax = time;
        }
        else if (field.endTimeField && (!this.timeRangeMin || (time.getTime() != this.timeRangeMin.getTime()))) {
            var end = me.down('datefield[name=' + field.endTimeField + ']');
            end.minValue = time;
            end.validate();
            this.timeRangeMin = time;
        }
        return true;
    }
});

以上mixin的示例用法

这是表格的部分内容。

    {
        xtype: 'datefield',
        fieldLabel: 'From',
        name: 'from_date',
        format: 'Y-m-d',
        editable: false,

        endTimePeriod:'to_date', //below component
        validationEvent: 'change',
        validator: function (value) {
            return me.timerange(value, this);
        }
    },
    {
        xtype: 'datefield',
        fieldLabel: 'To',
        format: 'Y-m-d',
        name: 'to_date',
        editable: false,

        startTimeField:'from_date', //above component
        validationEvent: 'change',
        validator: function (value) {
            return me.timerange(value, this);
        }
    }

1 个答案:

答案 0 :(得分:1)

日期范围验证的自定义Vtype日期范围:

Ext.apply(Ext.form.VTypes, {
    daterange : function (val, field) {
        var date = field.parseDate(val);
        if (!date) {
            return false;
        }
        if (field.startDate && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) {
            var start = Ext.getCmp(field.startDate); //field.up('form').down('#'+field.startDate);
            start.setMaxValue(date);
            start.validate();
            this.dateRangeMax = date;

        } else if (field.endDate && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) {
            var end = Ext.getCmp(field.endDate); //field.up('form').down('#' + field.endDate);
            end.setMinValue(date);
            end.validate();
            this.dateRangeMin = date;
        }
        return true;
    },
    daterangeText : Ext.BUNDLE.getMsg('Label.daterangetext')
});

从日期和到日期组件

{
    xtype : 'panel',
    items : [{
            xtype : 'displayfield',
            value : 'From Date'
        }, {
            id : 'msgFromDate',
            xtype : 'datefieldplus',
            vtype : 'daterange',
            endDate : 'msgToDate'
        }
    ]
}, {
    xtype : 'panel',
    items : [{
            xtype : 'displayfield',
            value : 'To Date'
        }, {
            id : 'msgToDate',
            xtype : 'datefieldplus',
            vtype : 'daterange',
            startDate : 'msgFromDate'
        }
    ]
}