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

时间:2016-01-13 04:41:38

标签: php yii2

我有以下代码:

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

结果是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>

我的问题是:

如何添加课程&#39;隐形&#39;到外部div(当前包含class = form-group)?

感谢您的帮助

2 个答案:

答案 0 :(得分:23)

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

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

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

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

您还可以有条件地构建fieldConfig

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

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

官方文档:

答案 1 :(得分: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>

对于自定义字段,您可以按

定义类
$form->field($model, 'phone_no', [
          'options' => [
             'class' => 'form-group invisible'
           ])->textInput([
              'placeholder' => '(Conditionally validated based on checkbox above, groovy!)']) ?>