yii场景使用
································场景1································
控制器:
$model->scenario='update';//指定场景 $model -> save();
模型中:
public function rules() { return [ ['name','required','message'=>'用户名不能为空'], ['age','required','message'=>'年龄不能为空','on'=>'create'], ['pwd','required','message'=>'密码不能为空','on'=>'update'] ]; }
结果:
验证name和pwd
································场景2································
控制器:
$model->scenario='update'; $model -> save();
模型中:
public function rules(){ return [ ['name','required','message'=>'用户名不能为空'], ['age','required','message'=>'年龄不能为空','on'=>'create'], ['pwd','required','message'=>'密码不能为空','on'=>'update'], ['level','required','message'=>'等级不能为空','on'=>'update'], ]; } public function scenarios(){ return [ 'update'=>['pwd'], ] }
结果:
验证pwd
································场景3································
控制器:
$model -> save();
模型中:
public function rules(){ return [ ['name','required','message'=>'用户名不能为空'], ['age','required','message'=>'年龄不能为空','on'=>'create'], ]; } public function scenarios(){ return [ 'update'=>['pwd'], ]; }
结果:
报错,缺少场景
································场景4································
控制器:
$model -> save();
模型中:
public function rules(){ return [ ['name','required','message'=>'用户名不能为空'], ['level','required','message'=>'等级不能为空'], ['age','required','message'=>'年龄不能为空','on'=>'create'], ]; } public function scenarios(){ return [ 'update'=>['pwd'], 'default'=>['name'] ]; }
结果:
验证name
································场景5································
控制器:
$model -> save();
模型中:
public function rules(){ return [ ['name','required','message'=>'用户名不能为空'], ['age','required','message'=>'年龄不能为空','on'=>'create'], ]; }
结果:
验证name
相关推荐
WasteLand 2020-10-18
Allinputs 2020-08-30
Ashes 2020-06-14
caiyiii 2020-06-14
kxguan 2020-06-14
daillo 2020-06-14
一粒沙里的世界 2020-06-14
ruxingli 2020-06-14
csssy00 2020-06-14
阿佐 2020-06-14
NameWFY 2020-05-28
NameWFY 2020-05-26
Robin罗兵 2020-05-16
caiyiii 2020-04-29
wmsjlihuan 2020-04-26
cbao 2020-04-26
csssy00 2020-04-19
igogo00 2020-03-09