简体   繁体   English

电子邮件唯一验证器 yii2 不返回消息验证

[英]email unique validator yii2 doesn't return message validation

I am try to use unique validator for email in my model, but it doesn't work.. i mean no notification that display in form when user input the same email that already saved in db and user still can click submit button(and page reloaded) eventhough data isn't saved in db.我尝试在我的模型中对电子邮件使用唯一验证器,但它不起作用..我的意思是当用户输入已保存在数据库中的相同电子邮件时,表单中没有显示通知,并且用户仍然可以单击提交按钮(和页面重新加载)即使数据未保存在 db 中。 What's wrong with my code?我的代码有什么问题?

this is my model validation:这是我的模型验证:

['email', 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\application\common\models\User', 'message' => 'This email address has been taken.'],

this is my form :这是我的表格:

<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>

<?= $form->field($model,'email')->textInput()->input('email', ['placeholder' => "Email"])->label(false) ?>

...

<div class="form-group">
<?= Html::submitButton('SIGN UP NOW', ['class' => 'btn btn-sign', 'type' => 'submit']) ?>
</div>

<?php ActiveForm::end(); ?>

and this is my model to process save:这是我处理保存的模型:

if (!$this->validate()) {
     return null;
}

$user = new User();

$user->email = $this->email;
...

you should add a filter in your model validation.您应该在模型验证中添加过滤器。

['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique',
          'targetClass' => '\common\models\User',
           'message' => Yii::t('frontend', 'This email address has already been taken.')
        ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM