简体   繁体   中英

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. 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.')
        ],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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