在某些情况下,我希望 Yii 行为中的 beforeSave 能够中断保存并返回错误。我尝试过但没有成功的是:
public function beforeSave($event) {
parent::beforeSave($event);
$tested_value = null;
if(is_null($tested_value)){
$this->validationErrors = Yii::t('app', 'Ops! Error');
return false;
}
}
在 Controller 中:
$model=new Post;
if($model->save()){
// no matter what this is always executed
} else {
print_r($model->validationErrors);die;
}
最佳答案
据记录here :如果您在 CActiveRecordBehavior
中重写 beforeSave($event)
方法,则必须设置 $event 的
为 false,如果您想阻止保存所有者模型。isValid
属性
if($preventSave) {
$event->isValid = false;
}
另请注意,它是 $model->errors
,而不是 $model->validationErrors
。
关于php - 如何让Yii beforeSave中断保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19420366/