mrs
(Raahsalah)
1
I would like to add custom link in Yii 2 model based validation message.
I am using following code block at the moment-
public function rules()
{
return [
['email', 'required'],
['email', 'email'],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Email address has already been taken.'],
];
I want this message to display like following-
"Email address is taken. Already registered? Then log-in here."
How can I achieve this?
Have you tried to use html in ‘message’ property or it is escaped?
As for example:
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Email address is taken. Already registered? Then <a href="/login">log-in</a> here.'],
webin2015
(Webin2015)
3
above code is right, you also need to change in form also.
in user model:
['email', 'unique', 'targetClass' => '\frontend\models\User', 'message' => 'Email address is taken. Already registered? Then <a href="/login">log-in</a> here.',],
and in view form :
<?= $form->field($model, 'email', ['errorOptions' => ['class' => 'help-block' ,'encode' => false]])->textInput() ?>
it works in my demo project.
regards,
webin
1 Like
mrs
(Raahsalah)
4
Thanks for the great suggestions. Yes, the solution was perfect.
webin2015
(Webin2015)
5
hi,
welcome
mark as + when got the correct solution…
regards,
webin