3

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?

The Coder
  • 618
  • 3
  • 10
  • 22

1 Answers1

6

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()
Tony
  • 5,797
  • 3
  • 26
  • 22