Tweaking FormHelper output (add an icon)


I want to tweak the output of the FormHelper so that I can insert an image tag right after the input field:

<div class="input checkbox">
<input type="hidden" name="cond_ice" value="0">
<label for="cond-ice">
<input id="cond-ice" type="checkbox" name="cond_ice" value="1">
<img src="/img/ice.png">Add ice

The <img> part is what I want to add. Before Cake3 you could supply an ‘after’ and a ‘before’ parameter to FormHelper->input() which did exactly that.

I tried a template like this:

return [
  'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{after}}</div>'

then called the formhelper:

$this->Form->control('cond_ice', ['type' => 'checkbox', 'after' => $this->Html->image('ice.png')]);

But this only added an “after” attribute to the input tag:

<input id="cond-ice" type="checkbox" after="<img src="/img/ice.png">" />

Obviously I could create all the divs, labels, input and img tags separately, but that’s so much more code… Suggestions on how to do this using FormHelper->control()? CSS is also not an option.

Thanks! That was the missing link.