首页 > 解决方案 > 如何在 cakephp 中使用 Html->control() 添加类以选择标签

问题描述

有人知道如何添加类来选择标签吗?请帮我!

我目前有:

<?= $this->Form->control(
              'tutor_profile.birthday',
              [
                'empty' => 'Choose...',
                'minYear' => date('Y') - 100,
                'maxYear' => date('Y')
              ]
            );
            ?>

选择标签的创建方式如下:

<select name="tutor_profile[birthday][year]">...</select>
<select name="tutor_profile[birthday][month]">...</select>
<select name="tutor_profile[birthday][day]">...</select>

但我希望他们是这样的:

<select name="tutor_profile[birthday][year]" class="myClass">...</select>
<select name="tutor_profile[birthday][month]" class="myClass">...</select>
<select name="tutor_profile[birthday][day]" class="myClass">...</select>

我尝试添加['class' => 'myClass']但没有用

标签: cakephp

解决方案


<?= $this->Form->control(
    'tutor_profile.birthday',
    [
        'empty' => 'Choose...',
        'minYear' => date('Y') - 100,
        'maxYear' => date('Y'),
        'class' => 'myClass', // <-------
    ]
  );?>

https://github.com/cakephp/cakephp/blob/master/src/View/Helper/FormHelper.php#L155

'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',

{{attrs}}

  • 'id' => 'my_id'
  • 'class' => 'my_class'
  • '数据自定义' => '自定义值'
  • ...

https://book.cakephp.org/3.0/en/views/helpers/form.html#creating-form-controls


推荐阅读