How to add custom link in Yii 2 validation error message?

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




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

Thanks for the great suggestions. Yes, the solution was perfect.

hi,

welcome

mark as + when got the correct solution…

regards,

webin