How to disable wrap checkbox with .form-group in Yii2

Andrey Izman picture Andrey Izman · Apr 25, 2015 · Viewed 8k times · Source

How to disable wrap checkbox with div.form-group in Yii2 using yii\bootstrap\ActiveForm?

That I need (AdminLTE login page template):

<div class="row">
    <div class="col-xs-8">
        <div class="checkbox icheck">
            <label>
                <input type="checkbox"> Remember Me
            </label>
        </div>
    </div>
    <div class="col-xs-4">
        <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
    </div>
</div>

I tried:

<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
    ...
    <div class="row">
        <div class="col-xs-8">
            <?= $form->field($model, 'rememberMe')->checkbox(); ?>
        </div>
        <div class="col-xs-4">
            <?= Html::submitButton('Sign In', ['class' => 'btn btn-primary btn-block btn-flat', 'name' => 'login-button']); ?>
        </div>
    </div>
<?php ActiveForm::end(); ?>

It render to:

<div class="row">
    <div class="col-xs-8">
        <div class="form-group field-loginform-rememberme">
            <div class="checkbox">
                <label>
                    <input type="checkbox" name="LoginForm[rememberMe]" value="0"> Remember Me
                </label>
                <p class="help-block help-block-error"></p>
            </div>
        </div>
    </div>
    <div class="col-xs-4">
        <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
    </div>
</div>

Everything is good except the extra wrapper <div class="form-group field-loginform-rememberme">
How to remove this extra wrapper?

Answer

Denis Rudov picture Denis Rudov · Apr 26, 2015
$form->field($model,'field',[
     'options'=>
           [
             'tag'=>'div',
             'class'=>'checkbox icheck'
           ]
       ]
   )->checkbox();

or you can do it in next way

$field = $form->field($model,'field',[
     'options'=>
           [
             'tag'=>'div',
             'class'=>'checkbox icheck'
           ],
      'selectors'=>[
           'error'=>'span.error'
           ]
       ]
   );
   echo $field->begin();
  ?>
    <label><?php 
       echo \yii\helpers\Html::activeCheckbox($model,'field');
    ?></label>
  <?php
       echo \yii\helpers\Html::error($model,'field',
                       ['class'=>'error','tag'=>'span' ]);

 <?php 
        echo $field->end();

something like this.