这是我的表格:
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($invite, 'email')->textInput([
'id' => 'register-email',
'placeholder' => Yii::t('UserModule.views_auth_login', 'email')]);
?>
<?php echo $form->field($invite, 'check')->checkbox([
'id' => 'check',
'uncheck' => null])->label(
Yii::t('UserModule.views_auth_login', 'I have read and accept') . ' <a href="#">'
. Yii::t('UserModule.views_auth_login', 'Terms & Conditions') . '</a> '
. Yii::t('UserModule.views_auth_login', 'and')
. ' <a href="#">' . Yii::t('UserModule.views_auth_login', 'Privacy Policy')
. '</a>' . '.');
?>
<hr>
<?php
echo \humhub\widgets\AjaxButton::widget([
'label' => Yii::t('UserModule.views_auth_login', 'Register'),
'ajaxOptions' => [
'type' => 'POST',
'beforeSend' => new yii\web\JsExpression('function(){ setModalLoader(); }'),
'success' => 'function(html){ $("#globalModal").html(html); }',
'url' => Url::to(['/user/auth/login']),
],
'htmlOptions' => [
'class' => 'btn btn-primary', 'id' => 'registerBtn'
]
]);
?>
<?php ActiveForm::end(); ?>
规则:
public function rules()
{
return [
[['email'], 'required'],
[['email'], 'unique'],
[['email'], 'email'],
[['email'], 'unique', 'targetClass' => \humhub\modules\user\models\User::className(), 'message' => Yii::t('UserModule.base', 'E-Mail is already in use! - Try forgot password.')],
[['check'], 'required'],
[['check'], 'compare', 'compareValue' => 1, 'message'=>'bla-bla-bla'],
];
}
如果“电子邮件”不为空且有效并且选中“检查”,我如何才能使我的表单有效?现在问题是'检查'没有验证。感谢。
答案 0 :(得分:1)
删除compare
规则并更新所需规则,如下所示。
[['check'], 'required', 'requiredValue' => 1, 'message'=>'bla-bla-bla'],
答案 1 :(得分:0)
尝试以下方法:
在您的模型中添加此方法:
public function validateCheckWithEmail($attribute, $params)
{
if (empty($this->check) && !empty($this->email) && $this->validateAttribute($this, 'email')) {
$this->addError($attribute, $this->getAttributeLabel($attribute).' field cannon be empty.');
}
}
在您的规则中添加:
[['check'], 'validateCheckWithEmail', 'skipOnEmpty' => false, 'skipOnError' => false],