php 如何在 YII2 中向 ActiveField 的表单组 div 添加类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34758542/
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
How to add class to form-group div of ActiveField in YII2?
提问by gvanto
I have some code below:
我在下面有一些代码:
<?=
$form->field($model, 'phone_no')->textInput(
[
'placeholder' =>
'(Conditionally validated based on checkbox above, groovy!)'
]
)
?>
Which results in HTML:
结果是 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>
My Question is:
我的问题是:
How can I add a class 'invisible' to the outer div (containing class=form-group currently)?
如何向外部 div(当前包含 class=form-group)添加“不可见”类?
Thanks for help
感谢帮助
回答by arogachev
You can do it like this for single field:
对于单个字段,您可以这样做:
<?= $form->field($model, 'phone_no', ['options' => ['class' => 'form-group invisible'])
->textInput(['placeholder' => '(Conditionally validated based on checkbox above, groovy!)']) ?>
Globally (for all fields in form) it's possible like this:
全局(对于表单中的所有字段)可能是这样的:
<?php $form = ActiveForm::begin([
'fieldConfig' => ['options' => ['class' => 'form-group invisible']],
]); ?>
You can also build fieldConfig
conditionally:
您还可以fieldConfig
有条件地构建:
<?php $form = ActiveForm::begin([
'fieldConfig' => function ($model, $attribute) {
if (...) {
return ['options' => ['class' => 'form-group invisible']],
}
},
]); ?>
Note that you have to include form-group
class as well, because it's not merged with your custom one.
请注意,您还必须包含form-group
类,因为它不会与您的自定义类合并。
Official docs:
官方文档:
回答by chapskev
Define the template layout for all the input elements.
为所有输入元素定义模板布局。
<?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>
For a custom field you can define a class by
对于自定义字段,您可以通过以下方式定义类
$form->field($model, 'phone_no', [
'options' => [
'class' => 'form-group invisible'
])->textInput([
'placeholder' => '(Conditionally validated based on checkbox above, groovy!)']) ?>