In How to custom input field in yii2? it shows how you can change the entire template
when using $form->field($model, 'customer_name')
to generate the HTML for a field in an ActiveForm.
But is there a way to modify just the HTML generated by the {input}
tag in that template?
I would like to have Yii generate all the HTML for the label and error messages, but want to generate the actual input field myself (eg. due to limitations in Yii's dropDownList()
features).
Extend ActiveField
class (in my case \yii\bootstrap4\ActiveField
) and override render
function, then set $this->parts['{input}'
with your custom code. e.g:
<?php
namespace common\widgets;
class MyCustomInput extends \yii\bootstrap4\ActiveField {
public function render($content = null) {
$this->parts['{input}'] = '<h3>This is my custom code for input.</h3>';
return parent::render($content);
}
}
NOTE: Remember you can get field's attributes using methods like:
\yii\helpers\Html::getInputName($this->model, $this->attribute)
or
$this->getInputId()