我正在使用yii2 bootstrap活动表单,我需要在一个表单组中保留多个输入。
喜欢
<div class="form-group field-phn required">
<label class="control-label" >Home Phone</label>
<select id="dialCode" class="form-control" name="AddPatientForm[home_dial_code]">
<option value="2">355,ALB</option>
<option value="3">213,DZA</option>
<option value="6">244,AGO</option>
<option value="224">971,ARE</option>
</select>
<input type="text" id="home_phn" class="form-control" name="AddPatientForm[home_phn]">
<p class="help-block help-block-error"></p>
</div>
我正在尝试的PHP代码是
<?php echo \yii\bootstrap\Html::activeLabel($objAddPatientFrm, 'home_phn') ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_dial_code', [ 'inputOptions' => [ 'id' => 'dialCode']])->dropDownList($dialCodeArray)->label(false); ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_phn', [ 'inputOptions' => [ 'id' => 'home_phn']]); ?>
但输出是
<label for="addpatientform-home_phn">Home Phn</label> <div class="form-group field-dialCode required">
<select id="dialCode" class="form-control" name="AddPatientForm[home_dial_code]">
<option value="2">355,ALB</option>
<option value="3">213,DZA</option>
<option value="6">244,AGO</option>
<option value="224">971,ARE</option>
</select>
<p class="help-block help-block-error"></p>
</div>
<div class="form-group field-home_phn required">
<input type="text" id="home_phn" class="form-control" name="AddPatientForm[home_phn]">
<p class="help-block help-block-error"></p>
</div>
将输入和标签保持为单独的形式组。
请建议我该怎么办?
答案 0 :(得分:1)
使用以下方式显示表单:
// With 'default' layout you would use 'template' to size a specific field:
echo $form->field($model, 'demo', [
'template' => '{label} <div class="row"><div class="col-sm-4">{input}{error}{hint}</div></div>'
]);
// Input group
echo $form->field($model, 'demo', [
'inputTemplate' => '<div class="input-group"><span class="input-group-addon">@</span>{input}</div>',
]);
答案 1 :(得分:0)
我对此的解决方案是防止$form->field()
呈现自己的form-group
类。这是通过使用options
属性并将options.class
设置为空字符串(或包括form-group
以外的任何类)来实现的。然后将字段包装在<div class="form-group">
中。
代码如下:
<div class="form-group">
<?= $form->field($Model, 'attribute1', ['options' => ['class' => '']]); ?>
<?= $form->field($Model, 'attribute2', ['options' => ['class' => '']]); ?>
</div>
对于OP的示例:
<div class="form-group field-phn required">
<?php echo \yii\bootstrap\Html::activeLabel($objAddPatientFrm, 'home_phn') ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_dial_code', [ 'inputOptions' => [ 'id' => 'dialCode'], 'options' => ['class' => '']])->dropDownList($dialCodeArray)->label(false); ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_phn', [ 'inputOptions' => [ 'id' => 'home_phn'], 'options' => ['class' => '']]); ?>
</div>