N
N
Nemesys2015-04-19 19:38:19
Yii
Nemesys, 2015-04-19 19:38:19

Yii2 Active Form: how to hide error tag if there is no error text?

I am using Yii2. We have an Active Form. The form is listing the fields, here is one field for example:

<?= $form->field($model,  'name', [
    template' => "
        {label}
        {input}
        <span class=\"error\">{error}</span>"
]) ?>

This will give me the following markup:
<label class="control-label" for="signupform-name">Name</label>
<input type="name" id="signupform-name" class="form-control" name="SignupForm[name]">
<span class="error">Ошибка! Поле не заполнено</span>

at the same time, if there is no error, then span.error will still be displayed (without text):
<label class="control-label" for="signupform-name">Name</label>
<input type="name" id="signupform-name" class="form-control" name="SignupForm[name]">
<span class="error"></span>

Question: is it possible to make it so that if there is no error in the field, then the empty span.error container is not displayed (and is displayed only when there is an error)?
Desired result if the error did NOT occur:
<label class="control-label" for="signupform-name">Name</label>
<input type="name" id="signupform-name" class="form-control" name="SignupForm[name]">

Desired result if an error occurs:
<label class="control-label" for="signupform-name">Name</label>
<input type="name" id="signupform-name" class="form-control" name="SignupForm[name]">
<span class="error">Возникла какая-то ошибка!</span>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2015-04-19
@IvanCher

<?= $form->field($model,  'name', [
    template' => "
        {label}
        {input}
        {error}",
    'errorOptions' => "
        'tag' => 'span',
        'class' => 'error',
    "
]) ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question