Custom templates using 'radioContainer' in Cakephp 3

1.9k views Asked by At

My default radio inputs are like this

$this->Form->radio('email_notifications', ['one','two']);

I have this in default:

<input type="radio" id="usertype-0" value="0" name="userType">
<label for="email-notifications-0">one</label>

And need to rephrase this structure to:

<label class="radio" for="usertype-0">
    <input type="radio" id="usertype-0" value="0" name="userType">one
</label>

Had tried with custom templates. It works fine with 'inputContainer', but not 'radioContainer'. Had used the following to test, by adding a div for radio buttons:
$this->Form->templates([ 'radioContainer' => '<label {{attrs}}">{{input}}{{text}}</div>' ]);
But nothing work for me as I get only the default structure. Am I doing someway wrong? I tried to configure with reference to this too: Cakephp 3 multiple custom template formhelpers

1

There are 1 answers

4
ndm On BEST ANSWER

I'm not sure what makes you think there would be a template named radioContainer, maybe this existed in earlier versions? The radio element related templates are radio and radioWrapper.

Anyways, the default output looks different to what you are showing, by default radio elements are nested in label elements, ie exactly what you are looking for according to your sample code.

This

$this->Form->radio('email_notifications', ['one','two']);

will generate the following HTML (indentations and linebreaks added by me)

<input type="hidden" name="email_notifications" value="">
<label for="email-notifications-0">
    <input type="radio" name="email_notifications" value="0" id="email-notifications-0">one
</label>
<label for="email-notifications-1">
    <input type="radio" name="email_notifications" value="1" id="email-notifications-1">two
</label>

So the fix should be to update to the latest (dev) version.