rtlm
(Sean Clarke)
October 3, 2016, 10:10am
1
Hi all,
Is it possible when creating an ActiveField to specify which DOM element the label text populates?
I have the following:
<label class="b-form__fieldContainer">
<span class="b-form__fieldName" id="labelElement"></span>
<span class="b-form__fieldWrapper">
<span class="b-form__singleField">
<?php
echo $form->field($model, 'attribute')
->textInput();
?>
</span>
</span>
</label>
What I would like to do is specify that the element "#labelElement " is populated with the text retrieved from the model.
The only way I can make it work at the moment is to set "label(false)" on the ActiveField and manually populate the "#labelElement " using a server side call.
Any help greatly appreciated.
Thanks.
Vojtech
(Vojtech Horak)
October 3, 2016, 12:37pm
2
ActiveField only render a HTML code so I cannot imagine how it would be possible to specify the DOM element (without using JS).
Nevertheless, there is a lot of ways how you can customize the generated HTML code. Perhaps it will work for your needs as well.
Here is an example how yii\bootstrap\ActiveForm (yii\bootstrap\ActiveField) can be customized:
<?php $form = ActiveForm::begin([
'fieldConfig' => [
'template' => "<label class=\"sample-a\">{labelTitle}</label>\n{hint}\n{input}\n{error}",
'checkboxTemplate' => "<div class=\"sample-b\">{input}\n{hint}\n{labelTitle}\n{error}</div>",
]
]) ?>
This configures all fields of the for. Individual fields can be configured as well.
Vojtech
(Vojtech Horak)
October 3, 2016, 12:41pm
3
Sometimes when you need to customize how items are rendered you can use something like this:
'item' => function ($index, $label, $name, $checked, $value) {
if (is_array($label)) {
$content = Html::radio($name, $checked, [
'value' => $value,
'data' => ['points' => $label[1]],
]) . ' ' . $label[0];
} else {
$content = Html::radio($name, $checked, [
'value' => $value,
]) . ' ' . $label;
}
return Html::label($content);
},