php 如何在 Yii 2.0 框架中为 ActiveForm 添加类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25718696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:58:11  来源:igfitidea点击:

How to add class to ActiveForm in Yii 2.0 framework?

phpcssyii2yii-widgets

提问by paus

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?

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

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>

回答by Alec Smythe

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

在 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();
?>

回答by Kartz

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

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

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

Please read this linkfor further clarification.

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

回答by emte

You can use htmlOptions:

您可以使用htmlOptions

<?php
    $form = ActiveForm::begin(
        [
            'action' => '/login',
            'htmlOptions' => [
                'class' => 'userform'
             ]
        ]
    );
    // ... add all your inputs here for example:
    echo $form->field($model, 'login');
    ActiveForm::end();
?>

回答by Luka Camernik

My first answer but in widget options add

我的第一个答案,但在小部件选项中添加

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

'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

我似乎没有正确阅读问题,我发布了 Yii 1.x

for Yii 2.0

对于 Yii 2.0

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

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

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

回答by Suraj Rathod

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

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

$form = ActiveForm::begin(['options' => ['class' => 'search-form clearfix']]);

回答by Suhas Bachhav

Optionsis worked for me.

选项对我有用。

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

I have referred this

我已经提到了这个