单选按钮 CakePHP 3.0

Radio Button CakePHP 3.0

在 CakePHP 2.0 中,我实际上可以将 'before'、'after' 和 'separator' 属性添加到单选按钮。这些属性将在我的单选选项之间创建一个 div 元素。这些选项似乎已从 CakePHP 3.0 中删除。我如何在 CakePHP 3.0 中做到这一点?

<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
    <div class="square-screening single-row screen-radio">
      <?php echo $this->Form->input('q1',array(
          'legend'=> false,
          'type'=>'radio',
          'options'=> $options,
          'required'=>'required',
          'before' => '<div class="radio-inline screen-center screen-radio">',
          'separator' => '</div><div class="radio-inline screen-center screen-radio">',
          'after' => '</div>',
      )); ?>
    </div>
</div>

非常简单。

您可以使用表单助手。

Cake\View\Helper\FormHelper::radio(string $fieldName, array $options, array $attributes)

使用

echo $this->Form->radio(
    'favorite_color',
    [
        ['value' => 'r', 'text' => 'Red', 'style' => 'color:red;'],
        ['value' => 'u', 'text' => 'Blue', 'style' => 'color:blue;'],
        ['value' => 'g', 'text' => 'Green', 'style' => 'color:green;'],
    ]
);

Docs

您需要使用 FormHelper 模板。来自迁移指南:

The separator, between, and legend options have been removed from radio(). You can use templates to change the wrapping HTML now.

The div, before, after, between and errorMessage options have been removed from input().

所以在你的情况下使用这个

echo $this->Form->input('q1', [
    'templates' => [
        'radioWrapper' => '<div class="radio-inline screen-center screen-radio">{{label}}</div>'
    ],
    'type' => 'radio',
    'options' => $options,
    'required' => 'required',
    'label' => false
]);

另请参阅: