yii2yii2-advanced-appyii2-basic-appyii2-modelyii2-validation

How to display a HTML tag in Yii2 form error summary


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


Solution

  • 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]) ?>