How to display a HTML tag in Yii2 form error summary

12k views Asked by At

I am trying to display a link in error message in login for, but it is not working.

The error message in LoginForm valdiation:

$this->addError($attribute, 'Your account has been disabled. <a href=""> Enable It</a>');

In login.php (view):

<?= $form->errorSummary($model); ?>

I tried like below, but not working:

 <?= $form->errorSummary($model,['errorOptions' => ['encode' => false,'class' => 'help-block']]); ?>

I am getting the following output instead of rendered a tag:

error summary

1

There are 1 answers

1
rob006 On BEST ANSWER

You need to disable encoding at ActiveForm level using encodeErrorSummary property, if you want to use $form->errorSummary($model):

<?php $form = ActiveForm::begin([
    'id' => 'login-form',
    'encodeErrorSummary' => false,
    'errorSummaryCssClass' => 'help-block',
]) ?>

<?= $form->errorSummary($model) ?>

Alternatively you may use Html::errorSummary() directly:

<?= Html::errorSummary($model, ['encode' => false]) ?>