Forms Additional Options

How is it possible in the options for the $form->field to add a div or class to the form input so I can have an inline label as opposed to a label above the input.

I have seen this -

and although the labelOptions adds the css to the label, i cannot get it to do anything to the form input.



IGNORE was using the from ActiveForm widget, should of been using the bootstrap version.

If there is any information on how to adjust each input so they have different layouts that would be great.

You can play with the template property of ActiveField to do this.

OR if you want you can directly use one of these extensions:

  1. ActiveForm in yii2-widgets. Just set the autoPlaceholder to true, this will convert all labels to inline placeholders (works for text inputs and text areas). You can also control bootstrap form layouts in here.

use kartik\widgets\ActiveForm;

$form = kartik\widgets\ActiveForm::begin([

    'fieldConfig' => ['autoPlaceholder'=>true]


  1. yii2-label-inplace - sets the labels to inline that pushes up or down once you enter the field

For Yii 1.1 I had to extend the form class and add a method to render a label, error and input with container divs etc as this is not natively supported there. I’ve also extended the methods for each form field type to set aria-* for better screen reader support. For example set attributes to tell screen readers to watch the error divs for content inserted via JS. I will start to port this over to 2.0. Eg. extending ActiveForm may be a solution if there are not config options available for your desired solution. Or in 2.0 the new Field thingy might be a class that you can extend and leave ActiveForm untouched.

Managed to get it working like this:

<?= $form->field($model, 'name', ['labelOptions' => ['class' => 'col-md-2'], 'horizontalCssClasses' => ['wrapper' => 'col-md-8'], ]) ?>

Using the bootstrap ActiveForm as opposed to the yii ActiveForm.