Yii2:ActiveForm:在一个字段上组合规则/多重验证 [英] Yii2: ActiveForm: combine rules / multiple validation on one field
问题描述
登录表单:
公共函数规则(){返回 [//用户名和密码都需要[['用户名', '密码'], '必填'],//用户名应该是一个数字和 8 位数字[['username'], 'number', 'message'=>'{attribute} 必须是数字'],[['用户名'], '字符串', '长度' =>8],//密码由validatePassword() 验证['密码','验证密码'],];}/*** 验证密码.* 此方法用作密码的内联验证.** @param string $attribute 当前正在验证的属性* @param array $params 规则中给出的附加名称-值对*/公共函数validatePassword($attribute, $params){如果 (!$this->hasErrors()) {$user = $this->getUser();if (!$user || !$user->validatePassword($this->password)) {$this->addError($attribute, '用户名或密码不正确.');}}}
我为上面看到的同一个字段设置了 2 条规则:
[['username'], 'number', 'message'=>'{attribute} 必须是数字'],[['用户名'], '字符串', '长度' =>8],
我希望表单针对以下 3 个场景情况显示不同的错误消息:
- 提供的值既不是数字,也不是 8 个字符(数字).
- 提供的值是一个数字,但不是 8 个字符(数字).
- 提供的值不是数字,而是 8 个字符(数字).
我的问题是 2 折:
A.有没有办法以任何标准,Yii2
方式组合这些规则.
B. 在我之前的问题中,我尝试设置自定义验证器(解决此问题的显而易见的方法),但它很简单地被忽略了.我可以使其验证的唯一方法是将 username
字段添加到场景中.但是,一旦我也添加了 password
,它又被忽略了.你能想到的任何原因吗?编辑:skipOnError = false
在这种行为中没有任何改变.
所以请在回答时确保最好在 yii2/advanced
中进行测试;我几乎没有接触过默认设置,所以应该很容易测试.
EDIT:为清楚起见,我只想允许 8 个字符(数字)的数字,因此它们可能有一个前导 0
,例如.00000001
或 00000000
就此而言.这就是为什么它必须是一个数字字符串.
针对不同情况组合规则和显示自定义错误消息的最佳方法是创建自定义验证器.现在,如果您也希望在客户端使用它(这是我在上面的问题 B 中详述的问题之一,感谢 @Beowulfenator 在这方面的领导),您必须创建一个实际的自定义验证器类扩展自 yii2 原生验证器类.
这是一个例子:
CustomValidator.php
<?php命名空间 app\components\validators;使用 Yii;使用 yii\validators\Validator;类 CustomValidator 扩展了 Validator{公共函数初始化(){父::init();}公共函数 validateAttribute($model, $attribute) {$model->addError($attribute, $attribute.' message');}公共函数 clientValidateAttribute($model, $attribute, $view){返回<<<JSmessages.push('$attribute message');JS;}}
登录表单.php
<?php命名空间 common\models;使用 Yii;使用 yii\base\Model;使用 app\components\validators\CustomValidator;/*** 登录表格*/类 LoginForm 扩展模型{公共 $ 用户名;公共 $password;公共 $custom;私人 $_user;/*** @inheritdoc*/公共函数规则(){返回 [//用户名和密码都需要[['用户名', '密码'], '必填'],//用户名应该是一个数字和 8 位数字[['username'], 'number', 'message'=>'{attribute} 必须是数字'],[['用户名'], '字符串', '长度' =>8],//密码由validatePassword() 验证['密码','验证密码'],['custom', CustomValidator::className()],];}//...
登录.php
<?php/* @var $this yii\web\View *//* @var $form yii\bootstrap\ActiveForm *//* @var $model \common\models\LoginForm */使用 yii\helpers\Html;使用 yii\bootstrap\ActiveForm;$this->title = '登录';?><div class="site-login text-center"><h1><?php echo Yii::$app->name;?></h1><?php $form = ActiveForm::begin(['id' =>'登录表格','fieldConfig' =>['模板' =>"{标签}\n{输入}"],'启用客户端验证' =>真的,'validateOnSubmit' =>真的,]);?><?= $form->errorSummary($model, ['header'=>'']) ?><div class="row"><div class="col-lg-4 col-lg-offset-4"><div class="col-lg-10 col-lg-offset-1"><div style="margin-top:40px"><?= $form->field($model, 'username') ?>
<div><?= $form->field($model, 'password')->passwordInput() ?>
<div><?= $form->field($model, 'custom') ?>
<div class="form-group" style="margin-top:40px"><?= Html::submitButton('Login', ['class' => 'btn btn-default', 'name' => 'login-button']) ?>