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

2k views Asked by At

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?

1

There are 1 answers

0
Tony On BEST ANSWER

As noted in the comment, you need to add link in to your message parameter, and also to prevent link from being encoded you need to set encode parameter to false.

$form->field($model, 'email', ['errorOptions' => ['class' => 'help-block' ,'encode' => false]])->textInput()