Radio Button CakePHP 3.0

时间:2015-06-18 00:29:49

标签: php cakephp 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>

2 个答案:

答案 0 :(得分:9)

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

  

已从radio()中删除了分隔符,之间和图例选项。您现在可以使用模板更改包装HTML。

     

div,before,after,between和errorMessage选项已从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
]);

另见:

答案 1 :(得分:1)

非常容易。

您可以使用表单助手。

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