24

I'd like to add a class to the form like:

<form role="form" action="/login" method="POST" class="userform">

How should I rewrite this for Yii 2.0 ActiveForm class?

The same question is for this structure inside of the form tag:

<div class="ui-grid-solo">
     <div class="ui-grid-a">
          <label for="name">Full Name</label>
          <input type="text" name="login" id="login" value="" data-clear-btn="true" data-mini="true">
          <label for="password">Password</label>
          <input type="password" name="password" id="password" value="" data-clear-btn="true" autocomplete="off" data-mini="true">
          <input type="checkbox" name="remind" id="remind" value="1">
          <label for="remind">Remember me</label>
          <br>
          <input type="submit" value="Login" onclick="this.form.submit();">
     </div>
</div>
userlond
  • 3,632
  • 2
  • 36
  • 53
Павел Иванов
  • 1,863
  • 5
  • 28
  • 51

6 Answers6

49

In Yii2 I don't think 'htmlOptions' works. Just 'options' is correct, e.g.

<?php
    $form = ActiveForm::begin(
        [
            'action' => '/login',
            'options' => [
                'class' => 'userform'
             ]
        ]
    );
    // ... add all your inputs here for example:
    echo $form->field($model, 'login');
    ActiveForm::end();
?>
Alec Smythe
  • 770
  • 7
  • 12
8

To add class in ActiveForm Yii2.0. You should use options

<?php $form = ActiveForm::begin(['action' => '/login','options' => ['class' => 'userform','enctype' => 'multipart/form-data']]); ?>

Please read this link for further clarification.

nshah143
  • 549
  • 4
  • 22
Kartz
  • 533
  • 3
  • 8
  • 22
4

You can use htmlOptions:

<?php
    $form = ActiveForm::begin(
        [
            'action' => '/login',
            'htmlOptions' => [
                'class' => 'userform'
             ]
        ]
    );
    // ... add all your inputs here for example:
    echo $form->field($model, 'login');
    ActiveForm::end();
?>
felipe.zkn
  • 2,012
  • 7
  • 31
  • 63
emte
  • 647
  • 1
  • 8
  • 25
2

My first answer but in widget options add

'htmlOptions'=>array('class'=>'editable)

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'my-form',
    'htmlOptions'=>array('class'=>'my-class'),
    'enableAjaxValidation'=>false,
)); ?>

I didn't read the question properly it seems, I posted for Yii 1.x

for Yii 2.0

'options'=>['class'=>'my-form']

$form = ActiveForm::begin(['id' => 'my-form', 'options'=>['class'=>'my-form']]);
1

Options is worked for me.

<?php
$form = ActiveForm::begin([
    'action' => '/login',
    'options' => [
        'class' => 'userform',
        'enctype' => 'multipart/form-data'
        ]
    ]);
?>

I have referred this

Suhas Bachhav
  • 403
  • 1
  • 7
  • 28
0

you can try with options to add class in active form for yii2

$form = ActiveForm::begin(['options' => ['class' => 'search-form clearfix']]);
Suraj Rathod
  • 170
  • 1
  • 12