Adding a custom part to ActiveField

Hi guys,

I’m trying to extend ActiveField to add a custom part to the template and have it then replaced with actual html output. I tried to code below, but the end result just shows {icon} instead of having it replaced using activeIcon. How do I go from here?

public function render($content = null) {
	$this->parts['{icon}'] = static::activeIcon();
 
	return parent::render();
}

public static function activeIcon() {
	return 'Some HTML output';
}

It’s probably something very obvious, but I’ve been staring at it too long I guess.

Hi, I just tested your code and it works. Check your form view file, maybe the problem is in how you are trying to render your ActiveField. It should be something like:

<?php $form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldClass' => '\app\widgets\MyField',
    'fieldConfig' => [
        'template' => "{label}\n{input}\n{hint}\n{icon}\n{error}"
    ]
]) ?>
    
<?= $form->field($model, 'username') ?>

Where 'fieldClass' is name of the class of your extended ActiveField.

1 Like

Right, thanks. Indeed it only works from template. Any other option won’t work (obviously in hindsight).