我有不同的表单用户注册和更改密码,但
public function rules()
{
return [
[['username', 'password_hash', 'email'], 'required'],
[['created_at', 'updated_at'], 'integer'],
[['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
[['auth_key'], 'string', 'max' => 32],
[['username'], 'unique'],
[['email'], 'unique'],
[['email'], 'email'],
[['password_reset_token'], 'unique'],
//rules for change Password
[['oldPassword', 'newPassword', 'confirmPassword'], 'required', 'on' =>'change_password'],
//check user input password is validate with database password
[['oldPassword'], 'validateOldPassword', 'on' =>'change_password'],
[['newPassword'], 'string', 'min' => 6 , 'on' => 'change_password'],
[['newPassword'], 'filter', 'filter' => 'trim', 'on' => 'change_password'],
[['confirmPassword'], 'compare', 'compareAttribute' => 'newPassword','message'=>'Password did not match', 'on' => 'change_password'],
];
}
我的问题是在我的控制器中获得这种不同的场景。 谢谢你。
答案 0 :(得分:3)
在模型中,您必须指定如下所示的方案。 例如,在注册名称时,将需要电子邮件和密码。 考虑仅登录时需要电子邮件和密码
<?php
public function rules(){
return [
[['email','password'],'required','on'=>'login'],
[['name', 'email', 'password'], 'required', 'on' => 'register'],
];
?>
<?php
class MainController extends Controller
{
public function actionLogin(){
$model = new User();
$model->scenario = 'login';
}
public function actionRegister(){
$model = new User();
$model->scenario = 'register';
}
}
?>
访问Scenarios链接