22

我想在表单中添加一个类,例如:

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

我应该如何为 Yii 2.0 ActiveForm 类重写这个?

对于表单标签内的这个结构,同样的问题是:

<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>
4

6 回答 6

48

在 Yii2 中,我认为 'htmlOptions' 不起作用。只是“选项”是正确的,例如

<?php
    $form = ActiveForm::begin(
        [
            'action' => '/login',
            'options' => [
                'class' => 'userform'
             ]
        ]
    );
    // ... add all your inputs here for example:
    echo $form->field($model, 'login');
    ActiveForm::end();
?>
于 2015-05-26T09:55:07.310 回答
8

在 ActiveForm Yii2.0 中添加类。你应该使用选项

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

请阅读此链接以获得进一步说明。

于 2014-12-02T09:20:16.070 回答
4

您可以使用htmlOptions

<?php
    $form = ActiveForm::begin(
        [
            'action' => '/login',
            'htmlOptions' => [
                'class' => 'userform'
             ]
        ]
    );
    // ... add all your inputs here for example:
    echo $form->field($model, 'login');
    ActiveForm::end();
?>
于 2014-09-08T12:09:03.490 回答
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']]);
于 2014-09-08T08:44:29.303 回答
0

选项对我有用。

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

我已经提到了这个

于 2018-09-18T05:44:58.970 回答
0

您可以尝试使用选项以活动形式为 yii2 添加类

$form = ActiveForm::begin(['options' => ['class' => 'search-form clearfix']]);
于 2018-09-18T05:33:52.387 回答