很抱歉这么晚才回答。在控制器中,您的意思是在我的操作中创建?之前$model->save
?我尝试过,但它似乎不起作用。我想在我的模型中创建一个函数,如下所示:
public function unique() {
if ($this->day1s == "00:00" || $this->day1s == "0:00") {
$this->day1s='';//here it seems to work for the 00:00
} else {
//but my rule gets ignored now for any other values. What can I add here to make it work?
}
也很抱歉发布答案而不是编辑我的问题。
我最终想通了:我的规则是这样的:
array('day1s,day1e, day2s, day2e, day3s, day3e, day4s, day4e, day5s, day5e, day6s, day6e, day7s, day7e','unique','message'=>'This already exist, please change', 'allowEmpty'=>true,
'criteria' => array(
'condition' => 'schoolid=:schoolid',
'params' => array(':schoolid' => $this->schoolid))
),
我的控制器是这样的:
if ($model->day1s == "00:00" || $model->day1s == "0:00") {
$model->day1s='';//for custom validation rule 'unique'
}
if ($model->day1e == "00:00" || $model->day1e == "0:00") {
$model->day1e='';
}
.....
if($model->validate()){
if($model->save())
$this->redirect('admin');}
}
再次感谢你的帮助!