php Yii2 ActiveForm 字段选项不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27475652/
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 23:23:54  来源:igfitidea点击:

Yii2 ActiveForm fields options does not work

phpyii2

提问by SaidbakR

According to the official tutorialof Yii2. I have created a view for the entry form:

根据Yii2的官方教程。我为输入表单创建了一个视图:

    <?php
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;
    ?>
    <?php $form = ActiveForm::begin(); ?>
<!-- GET Attention for the next Line -->
    <?= $form->field($model, 'name')->label('Your Name'); ?>
    <?= $form->field($model, 'email'); ?>
    <div class="form-group">
      <?=  Html::submitButton('Send!', ['class' => 'btn btn-primary']); ?>
    </div>    
    <?php ActiveForm::end(); ?>

At this point everything is well fine. However, when I try to use the parameter optionsof the fieldmethod as follows:

在这一点上,一切都很好。但是,当我尝试使用optionsfield方法的参数时:

<?= $form->field($model, 'name', ['style' => 'color:red'])->label('Your Name'); ?>

I have got the error:

我有错误:

Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: yii\widgets\ActiveField::style

未知属性 – yii\base\UnknownPropertyException

设置未知属性:yii\widgets\ActiveField::style

The official api documentationstated that method of ActiveForm takes a third parameter called options

官方 api 文档指出 ActiveForm 的方法采用称为选项的第三个参数

Could anybody explain me why this error has been occurred?!

有人能解释一下为什么会发生这个错误吗?!

回答by Mihai P.

Try

尝试

<?= $form->field($model, 'name')->textInput(['style' => 'color:red'])->label('Your Name'); ?>

It is a little hard to explain, when you do $form->field($model, 'name')without specifying the field type you are actually asking for a textInput. But that does not mean that you should ask from ->field(to take the params the same way as ->textInput(does. If you need to put some special params for the field you have to use the explicit ->textInput(['style' => 'color:red'])

有点难以解释,当您$form->field($model, 'name')没有指定您实际要求 textInput 的字段类型时。但这并不意味着您应该要求 from->field(以相同的方式获取参数->textInput(。如果您需要为该字段放置一些特殊参数,则必须使用显式->textInput(['style' => 'color:red'])

回答by tsanchev

Try this code:

试试这个代码:

<?= $form->field($model, 'name', [ 'options' => [ 'style' => 'color: red']])->label('Your Name'); ?>

You've got this error

你有这个错误

Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: yii\widgets\ActiveField::style

because there is no such property style. You should use 'options' and pass 'style' as sub-array

因为没有这样的属性样式。您应该使用“选项”并将“样式”作为子数组传递

回答by Jose Manuel Kun

This way is for especify options on all the "field" (that includes the "label" and the "input") but 'style'=>'color:red' only affects to label in this way, I use this for especify class options instead of color:

这种方式用于所有“字段”(包括“标签”和“输入”)上的指定选项,但 'style'=>'color:red' 仅影响以这种方式标记,我将其用于 especify 类选项而不是颜色:

<?= $form->field($model, 'name', [ 'options' => [ 'class' => 'col-xs-8']])->label('Your Name'); ?>

If you want to especify options for one of them (label or input)you can do it separately, like this:

如果您想为其中之一(标签或输入)指定选项,您可以单独进行,如下所示:

<?= $form->field($model, 'name')->textInput(['style' => 'color:red'])->label('Your Name',['style'=>'color:blue']); ?>

Yii is so flexible, that is what i like of it.

Yii 非常灵活,这就是我喜欢它的地方。