我的模型验证规则如下
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
//post of step 1
if (isset($_POST['yt0']))
{
return array(
array('EmailAddress,', 'required','message' => Yii::t('message', 'USERNAME_REQUIRED')),
array('Password,', 'required','message' => Yii::t('message', 'PASSWORD_REQUIRED')),
array('confirmPassword,', 'required','message' => Yii::t('message', 'CONFIRM_PASSWORD_REQUIRED')),
array('Password','length', 'max' => 100, 'min' => 6, 'tooShort' => Yii::t('message', 'PASSWORD_LENGTH')),
array('confirmPassword', 'compare', 'compareAttribute'=>'Password','message' => Yii::t('message', 'PASSWORD_COMPARE')),
array('EmailId,', 'required','message' => Yii::t('message', 'EMAILID_REQUIRED')),
array('EmailId','email','message'=>Yii::t('message', 'EMAILID_VALID')),
array('chapterCode,', 'required','message' => Yii::t('message', 'CHAPTERCODE_REQUIRED')),
array('verifyCode,', 'required','message' =>Yii::t('message', 'VERIFYCODE_REQUIRED')),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'caseSensitive'=>true,'message' =>Yii::t('message', 'VERIFYCODE_INCORRECT') ),
array('EmailAddress', 'unique','className'=>'User','attributeName'=>'EmailAddress','message'=>Yii::t('message', 'EMAILID_UNIQUE')),
array('PersonId', 'unique','className'=>'User','attributeName'=>'PersonId','message'=>"Person already exists."),
array('FailedLoginCount', 'safe'),
);
} elseif (isset($_POST['yt1']))
{
return array(
array('EmailAddress,', 'required','message' => Yii::t('message', 'USERNAME_REQUIRED')),
array('Password,', 'required','message' => Yii::t('message', 'PASSWORD_REQUIRED')),
array('FailedLoginCount', 'safe'),
);
}elseif (isset($_POST['savecontact'])||$this->memBelongsto==0)
{
return array(
array('EmailAddress,', 'required','message' => Yii::t('message', 'USERNAME_REQUIRED')),
array('Password,', 'required','message' => Yii::t('message', 'PASSWORD_REQUIRED')),
array('confirmPassword', 'required','message' => Yii::t('message', 'CONFIRM_PASSWORD_REQUIRED')),
array('Password','length', 'max' => 100, 'min' => 6,'tooShort' => Yii::t('message', 'PASSWORD_LENGTH')),
array('confirmPassword', 'compare', 'compareAttribute'=>'Password','message' => Yii::t('message', 'PASSWORD_COMPARE')),
array('EmailId,', 'required','message' => Yii::t('message', 'EMAILID_REQUIRED'),'except'=>'datavalid'),
array('EmailId','email','message'=>Yii::t('message', 'EMAILID_VALID'),'except'=>'datavalid'),
array('FailedLoginCount', 'safe'),
);
}else{
return array(
array('EmailAddress,EmailId, Password', 'required'),
array('PersonId, ActiveFlag, FailedLoginCount', 'numerical', 'integerOnly'=>true),
array('EmailAddress, Password', 'length', 'max'=>100),
array('confirmPassword,', 'required','message' => Yii::t('message', 'PASSWORD_REQUIRED')),
array('Password','length', 'max' => 100, 'min' => 6, 'tooShort' => Yii::t('message', 'PASSWORD_LENGTH')),
array('confirmPassword', 'compare', 'compareAttribute'=>'Password','message' => Yii::t('message', 'PASSWORD_COMPARE')),
array('chapterCode,', 'required','message' => Yii::t('message', 'CHAPTERCODE_REQUIRED')),
array('verifyCode,', 'required','message' =>Yii::t('message', 'VERIFYCODE_REQUIRED')),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'caseSensitive'=>true,'message' =>Yii::t('message', 'VERIFYCODE_INCORRECT')),
array('FailedLoginCount', 'safe'),
);
}
}
并且此处yt0
是注册页面上提交按钮的名称,因此,对于注册,将应用yt0
的规则,并且我有另一个更改密码的表单,因此在该页面上我只需要三个字段并且更改密码的提交按钮为savecontact
但是应用了savecontact规则,但注册按钮规则存在冲突,因此注册验证无效。如果正确的规则是正确的方式,请提出任何建议......
答案 0 :(得分:1)
虽然这可行,但你是以错误的方式做到的。
您应该使用scenario
来执行这些任务。
首先,您可以为不同的表单声明方案:
class User extends ActiveRecord
{
const SCENARIO_LOGIN = 'login';
const SCENARIO_SIGNUP = 'signup';
const SCENARIO_UPDATE = 'update';
}
完成定义方案后,您可以告诉您的模型使用哪些属性进行大规模分配并对特定方案应用验证。
class User extends ActiveRecord
{
const SCENARIO_LOGIN = 'login';
const SCENARIO_REGISTER = 'register';
const SCENARIO_UPDATE = 'update';
public function scenarios()
{
return [
self::SCENARIO_LOGIN => ['username', 'password'],
self::SCENARIO_REGISTER => ['email', 'username', 'password'],
self::SCENARIO_UPDATE => ['email', 'username', 'password'],
];
}
}
现在您已经完成了,您可以选择添加验证规则或使用由gii生成的默认规则。
class User extends ActiveRecord
{
const SCENARIO_LOGIN = 'login';
const SCENARIO_REGISTER = 'register';
const SCENARIO_UPDATE = 'update';
public function scenarios()
{
return [
self::SCENARIO_LOGIN => ['username', 'password'],
self::SCENARIO_REGISTER => ['email', 'username', 'password'],
self::SCENARIO_UPDATE => ['email', 'username', 'password'],
];
}
public function rules()
{
return ArrayHelper::merge([
['email' => 'email', 'message' => Yii::t('message', 'wrong email format')],
[['email', 'password', 'username'] => 'email'],
['username', 'exist', 'on' => self::SCENARIO_LOGIN], // this will apply only on login
// other rules
], parent::rules());
}
}
现在您已完成模型方案和规则,是时候正确使用它们了。
前往您的控制器并使用方案实例化您的模型:
public function actionLogin()
{
$model = new User(['scenario' => User::SCENARIO_LOGIN]);
if($model->load(Yii::$app->request->post())) {
// $model will have username and password even if email is in the form since it is using
// User::SCENARIO_LOGIN
$model->validate();
// will validate only username and password even if email is in the form
// will also use the username exist validator
// do your stuff
}
}
public function actionRegister()
{
$model = new User(['scenario' => User::SCENARIO_REGISTER]);
if($model->load(Yii::$app->request->post())) {
// $model will have username, password and email
$model->validate();
// will validate email, username and password but will not use the
// username exist validation rule since it has the `'on' => self::SCENARIO_LOGIN`
// do your stuff
}
}
转到http://www.yiiframework.com/doc-2.0/guide-structure-models.html了解有关如何正确使用模型和验证的更多详细信息