Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

forms - CakePHP 3 changing the radio input template

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')]
    ]);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...