首页 > 解决方案 > CakePHP 3.7.1 向表单选择控件选项添加类

问题描述

从文档中,我已经能够构建一个表单选择控件,但是,我不清楚如何将类添加到选择选项中......

这是我在 CakePHP 表单中的选择控件

<?= $this->Form->select('type', [
        'Value 1' => 'Name 1',
        'Value 2' => 'Name 2'
    ],[
        'class' => 'js-custom-select w-100 u-select-v2 u-shadow-v19 g-brd-none g-color-black g-color-primary--hover g-bg-white text-left g-rounded-30 g-pl-30 g-py-12',
        'data-open-icon' => "fa fa-angle-down", 
        'data-close-icon' => "fa fa-angle-up"
    ]
) ?>

这是我在 HTML 中的选择控件

<select name="type" class="js-custom-select w-100 u-select-v2 u-shadow-v19 g-brd-none g-color-black g-color-primary--hover g-bg-white text-left g-rounded-30 g-pl-30 g-py-12" data-placeholder="Type" data-open-icon="fa fa-angle-down" data-close-icon="fa fa-angle-up">
    <option class="g-brd-secondary-light-v2 g-color-black g-color-white--active g-bg-primary--active" value="value 1">Name 1</option>
    <option class="g-brd-secondary-light-v2 g-color-black g-color-white--active g-bg-primary--active" value="value 2">Name 2</option>
</select>

如何将选项中的类放入控件的 CakePHP 表单中?

标签: htmlcsscakephpcakephp-3.x

解决方案


要将类添加到您的选项中,您需要使用特定的选项数组结构:

$options = [
    [
        "text" => "Text to display for option 1",
        "value" => "Value to set for option 1",
        "class" => "Class list to set for option 1"
    ],
    [
        "text" => "Text to display for option 2",
        "value" => "Value to set for option 2",
        "class" => "Class list to set for option 2"
    ],
    /** ... **/
]

准备好这样的数组后,您可以将其与FormHelper::select()or一起使用FormHelper::control()

$this->Form->select("field_name",$options);

$this->Form->control("field_name",[
    "label" => "My Label",
    "type" => "select",
    "options" => $options
]);

推荐阅读