How to add class to form-group div of ActiveField

I have some below:


<?= $form->field($model, 'phone_no')

                            ->textInput(['placeholder'=>'(Conditionally validated based on checkbox above, groovy!)']) ?>



Which results in HTML:


<div class="form-group field-contactform-phone_no">

<label class="control-label">Phone No

<input type="text" aria-describedby="hint-contactform-phone_no" placeholder="(Conditionally validated based on checkbox above, groovy!)" name="ContactForm[phone_no]" id="contactform-phone_no" class=""></label>

<small class="error-box"></small>

<p class="help-text" id="hint-contactform-phone_no"></p>

</div>

My Question is:

How can I add a class ‘invisible’ to the outer div (containing class=form-group currently) ?

Thanks for help

Check ActiveForm::form() method in the API reference.

http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html

Probably:




<?= $form->field($model, 'phone_no', ['class' => 'invisible form-group'])

    ...



Hmm, I’m not very sure whether we have to add ‘form-group’ or not.

yup that works thanks!

Unfortunately, doesnt works

echo $form->field($model, 'email', ['class' => 'form-group'])->textInput(['autocomplete'=>"off"])->label( $emailLabel );

Hi @regist70,

It looks very strange. Could you give us the stack trace?
(No image preferred. Text please)

Deleted everything but broken code

use yii\helpers\Html;

//use yii\widgets\ActiveForm;
use yii\bootstrap\ActiveForm;

echo '<div class="user-form">';
    $form = ActiveForm::begin([]);
    
    //echo $form->field($model, 'email')->textInput(['autocomplete'=>"off",'maxlength' => true]);

    echo $form->field($model, 'email', ['class' => 'form-group'])->textInput(['autocomplete'=>"off"])->label( $emailLabel );

    echo '<div class="form-group">';
    echo Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']);
    echo '</div>';
    ActiveForm::end();
echo '</div>';

Still have error
Preformatted text # Not instantiable – [yii\di\NotInstantiableException](http://www.yiiframework.com/doc-2.0/yii-di-notinstantiableexception.html)

## Failed to instantiate component or class "form-group".

## Caused by: ReflectionException

### Class form-group does not exist

in G:\WebServers7.4\home\wash\www\vendor\yiisoft\yii2\di\Container.php at line 508

Full page with error report https://dropmefiles.com/C4wba

1 Like

Hi @regist70,

I’m very sorry. I was wrong.

Try this instead:

$form->field($model, 'email', ['options' => ['class' => 'invisible']])-> ...

The 3rd parameter of ActiveForm::field() is:

and so we have to use ActiveField::options to set the html attributes (e.g. ‘class’) of the field div.

1 Like

Yes, this is works.
Thank you.

this way worked for me.

Instead passing $options, i used the array [‘options’ => […] ]