如何在YII2中将类添加到ActiveField的form-group div?

gva*_*nto 15 php yii2

我在下面有一些代码:

<?=
  $form->field($model, 'phone_no')->textInput(
    [
      'placeholder' =>
      '(Conditionally validated based on checkbox above, groovy!)'
    ]
  )
?>
Run Code Online (Sandbox Code Playgroud)

哪个结果是HTML:

<div class="form-group field-contactform-phone_no">
  <label class="control-label">Phone No
  <input type="text" aria-describedby="hint-contactform-phone_no" placeholder="(Conditionally validated based on checkbox above, groovy!)" name="ContactForm[phone_no]" id="contactform-phone_no" class=""></label>
  <small class="error-box"></small>
  <p class="help-text" id="hint-contactform-phone_no"></p>
</div>
Run Code Online (Sandbox Code Playgroud)

我的问题是:

如何在外部div中添加一个"隐形"类(当前包含class = form-group)?

感谢帮助

aro*_*hev 23

对于单个字段,您可以这样做:

<?= $form->field($model, 'phone_no', ['options' => ['class' => 'form-group invisible'])
    ->textInput(['placeholder' => '(Conditionally validated based on checkbox above, groovy!)']) ?>
Run Code Online (Sandbox Code Playgroud)

在全球范围内(对于表格中的所有字段),可能是这样的:

<?php $form = ActiveForm::begin([
    'fieldConfig' => ['options' => ['class' => 'form-group invisible']],
]); ?>
Run Code Online (Sandbox Code Playgroud)

您还可以fieldConfig有条件地建立:

<?php $form = ActiveForm::begin([
    'fieldConfig' => function ($model, $attribute) {
        if (...) {
            return ['options' => ['class' => 'form-group invisible']],
        }
    },
]); ?>
Run Code Online (Sandbox Code Playgroud)

请注意,您还必须包含form-group类,因为它未与您的自定义类合并.

官方文档:


cha*_*kev 6

为所有输入元素定义模板布局.

<?php
                $form = ActiveForm::begin([
                            'id' => 'purchase-sms-temp-form',
                            'layout' => 'horizontal',
                            'fieldConfig' => [
                                'template' => " <div class=\"form-group form-md-line-input\">{label}\n{beginWrapper}\n{input}<div class=\"form-control-focus\"> </div>\n{error}\n</div>{endWrapper}",
                                'horizontalCssClasses' => [
                                    'label' => 'col-md-2 control-label',
                                    'offset' => 'col-sm-offset-4',
                                    'wrapper' => 'col-sm-10',
                                    'error' => 'has-error',
                                    'hint' => 'help-block',
                                ],
                            ],
                ]);
                ?>

                <div class="form-body">
                    <?= $form->field($model, 'mobile') ?>
                    <?= $form->field($model, 'volume') ?>
                    <?= $form->field($model, 'hospital_id') ?>
                    <?= $form->field($model, 'created_date') ?>
                    <?= $form->field($model, 'complete') ?>
                    <?= $form->field($model, 'modified_date') ?>

                </div>
Run Code Online (Sandbox Code Playgroud)

对于自定义字段,您可以通过定义类

$form->field($model, 'phone_no', [
          'options' => [
             'class' => 'form-group invisible'
           ])->textInput([
              'placeholder' => '(Conditionally validated based on checkbox above, groovy!)']) ?>
Run Code Online (Sandbox Code Playgroud)