将类添加到cakehp3中单选按钮的标签中


Add class to the label for radio button in cakephp 3

在cakehp3中,我在向单选按钮标签添加类时遇到了一个问题。

我的代码是:

<?= $this->Form->input('enroll',[
    'type'=>'radio',
    'options'=>[
        [
            'value'=>'1',
            'text'=>'Yes',
            'class'=>'checkbox'
        ],
        [
            'value'=>'0',
            'text'=>'No',
            'class'=>'checkbox'
        ]
    ],
    'label'=>false
  ])
?>

这将生成以下Html:

<div class="input radio">
    <input type="hidden" value="" name="enroll">
    <label for="enroll-1">
        <input type="radio" id="enroll-1" class="checkbox" value="1" name="enroll">
        Yes
    </label>
    <label for="enroll-0">
        <input type="radio" id="enroll-0" class="checkbox" value="0" name="enroll">
        No
    </label>
</div>

我想要的是:

<div class="input radio">
<input type="hidden" value="" name="enroll">
<label for="enroll-1" class="checked-input">
    <input type="radio" id="enroll-1" class="checkbox" value="1" name="enroll">
    Yes
</label>
<label for="enroll-0" class="checked-input">
    <input type="radio" id="enroll-0" class="checkbox" value="0" name="enroll">
    No
</label>

我希望在<label for=''>中添加一个类。

我尝试的是:

<?= $this->Form->input('enroll',[
    'type'=>'radio',
    'templates'=>[
        'label'=>'<label {{attrs}} class="checked-input">{{text}}</label>',
        'radioWrapper'=>'{{label}}'
    'options'=>[
        [
            'value'=>'1',
            'text'=>'Yes',
            'class'=>'checkbox'
        ],
        [
            'value'=>'0',
            'text'=>'No',
            'class'=>'checkbox'
        ]
    ],
    'label'=>false
  ])
?>

但我做不到。我试过很多其他的方法,但都不起作用。请帮忙。

只需修改nestingLabel模板而不是label模板

'nestingLabel' => '{{hidden}}<label{{attrs}} class="checked-input">{{input}}{{text}}</label>'

这也是有效的。

<?= $this->Form->input('current_password', [
                'type' => 'password',
                'required' => true,
                'label' => ['class' => 'hello']]);
        ?>