Validate date of one column greater than with another column in yii

时间:2017-12-18 07:30:26

标签: yii

I have a form which has date_of_purchasing and expiry_date. I need to check date_of_purchasing should not be greater than expiry_date. How can i write this validation rule in model.

Im using YII framework. And im new for this framework.

2 个答案:

答案 0 :(得分:1)

Yii2

如果比较的列来自同一个表,这应该可以工作。

public function rules()
{
    return [
            [
                ['date_end'], 
                'compare', 
                'compareAttribute' => 'date_start', 
                'operator' => '>=', 
                'type' => yii\validators\DateValidator::TYPE_DATE, 
                'message' => 'Date end has to be greater than date start', 
                'enableClientValidation' => false
            ],
            // other rules...
    ];
}

不要忘记将 enableClientValidation 设置为 false,因为 Yii2 无法在没有服务器端请求的情况下检查其他日期。

其他配置选项here

<块引用>

附注。我决定在 3 年多之后更新这个帖子,因为它仍然存在 Google 搜索中针对此参数的第一个 StackOverflow 结果之一。

答案 1 :(得分:0)

尝试在此链接中使用CompareValidatorCompare

相关问题