验证中的方案
使用场景,你可以在不同情况下执行验证
在模型类中定义场景
class User extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'user_master';
}
// define validation in rule() function
public function rules()
{
return [
[['email_id'],'email'],
[['first_name',],'required','on'=>['create','update']], // create scenario
[['email_id',],'required','on'=> ['admin','create','update','forgotpassword']],
[['mobile',],'required','on'=>['admin','create','update']],
];
}
}
在 Controller 中使用场景
public function actionCreate()
{
$model = new User();
$model->scenario="create"; // use create scenario, create scenario validaion applied in this model
}
public function actionUpdate()
{
$model = new User();
$model->scenario="update"; // use update scenario, update scenario validaion applied in this model
}