本文介绍了Yii min 日期验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在模型中添加最短日期验证规则?
How can I add a minimum date validation rule in a Model?
示例:
我有一列 dt_ini 作为 DATE,我需要在创建时将输入限制为 D+7.
I have a column dt_ini as DATE, which I need to restrict the input as D+7, on create.
如果今天是:2012 年 10 月 1 日
If today is: october 1st, 2012
创建的最短有效日期为:2012 年 10 月 8 日.
the minimum valid date on create would be: october 8th, 2012.
否则,我会抛出一个验证错误:您的日期必须至少距现在 7 天.
Otherwise, I would throw a validation error as: Your date must be at least 7 days from now.
我期望的代码是这样的:(这没有经过测试,可能不会工作)
The code I would expect is something like this: (this is NOT tested, and probably won't work)
public function rules(){
return array('dt_ini', 'date', 'minDate' => '+7'),
}
谢谢.
推荐答案
创建自定义有效规则如下:
Create custom valid rule as follows:
class YourModel extends CActiveModel
{
// some....
public function rules(){
return array('dt_ini', 'dateValid', 'minDate' => '+7 day', 'on' => 'create');
}
public function dateValid($attribute, $params)
{
$valid=null;
$today = date('Y-m-d', time());
if(isset($params['minDate']))
$valid = date('Y-m-d', strtotime($params['minDate'])); //+7 day
if( !is_null($valid) )
{ //for increamental date
if($this->dt_ini > $valid || $this->dt_ini < $today )
$this->addError($attribute, 'enter error message');
}
}
}
这篇关于Yii min 日期验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!