5

Cakephp 3 create a radio container with label -> input like that

<div class="radio">
    <label class="radio-acces-checked" for="condition-access-1">
      <input id="condition-access-1" type="radio" value="1" name="condition_access">
      Free access
    </label>
</div>
...

I would like change structure but it does not work, it's always the same strucure... Do you have an idea about how to solve my problem ?

$myTemplates = [
  'radioWrapper' => '<div class="radio">{{label}}{{input}}</div>'
];
echo $this->Form->radio('condition_access', [
      ['value' => 1, 'text' => __('Free Access')],
      ['value' => 2, 'text' => __('Payment Access')],
      ['value' => 3, 'text' => __('Reduce price')]
    ]);
Jonas
  • 121,568
  • 97
  • 310
  • 388
jlafforgue
  • 287
  • 2
  • 5
  • 15

1 Answers1

11

You need to set the nestingLabel template:

echo $this->Form->input('condition_access', [
    'type' => 'radio',
    'options' => [
        ['value' => 1, 'text' => __('Free Access')],
        ['value' => 2, 'text' => __('Payment Access')],
        ['value' => 3, 'text' => __('Reduce price')]
    ],
    'templates' => [
        'nestingLabel' => '{{hidden}}<label{{attrs}}>{{text}}</label>{{input}}',
        'radioWrapper' => '<div class="radio">{{label}}</div>'
    ]
]);

Output:

<div class="input radio">
    <label>Condition Access</label>
    <input name="condition_access" value="" type="hidden">
    <div class="radio">
        <label for="condition-access-1">Free Access</label>
        <input name="condition_access" value="1" id="condition-access-1" type="radio">
    </div>
    <div class="radio">
        <label for="condition-access-2">Payment Access</label>
        <input name="condition_access" value="2" id="condition-access-2" type="radio">
    </div>
    <div class="radio">
        <label for="condition-access-3">Reduce price</label>
        <input name="condition_access" value="3" id="condition-access-3" type="radio">
    </div>
</div>
Holt
  • 36,600
  • 7
  • 92
  • 139
  • Is there any way to place some HTML in the templates so I could do something better in labels? http://book.cakephp.org/3.0/en/views/helpers/form.html#moving-checkboxes-radios-outside-of-a-label {{input}} – Patrice Poliquin Oct 06 '16 at 14:03
  • @PatricePoliquin You can add whatever you want in templates, as long as it does not mess up with the template engine (e.g. avoid using `{{` or `}}`). – Holt Oct 06 '16 at 14:32
  • @Holt Would it be possible for me to add additonnal HTML after each {{text}} () ? – Patrice Poliquin Oct 26 '16 at 15:15