我有一个管理表单,其中包含在我的_prepareForm方法中声明的几个 Select 字段,如下例所示:

protected function _prepareForm()
{
    $form = new Varien_Data_Form(array(
        'id' => 'datacenter_action_form',
        'action' => $this->getUrl('*/*/saveConfig'),
        'method' => 'post',
        'enctype' => 'multipart/form-data'
    ));
    $fieldset = $form->addFieldset('base_fieldset', array('legend' => 'Example');
    $fieldset->addField('entity', 'select', array(
        'name' => 'action[entity]',
        'title' => 'Entity',
        'label' => 'Entity',
        'required' => true,
        'values' => array('one','two')
    ));

    ...

    return parent::_prepareForm();
}

我想知道是否可以像在source models中一样,通过嵌套值将optgroups加到字段值中,如下所示:

...
$fieldset->addField('entity', 'select', array(
        'name' => 'action[entity]',
        'title' => 'Entity',
        'label' => 'Entity',
        'required' => true,
        'values' => array('value' => array('one','two'), 'label' => 'Numbers')
    ));
...

其中,预期输出为:

<select>
    <optgroup label="Numbers">
        <option>one</option>
        <option>two</option>
    </optgroup>
</select>

OBS:我已经try 了以与源模型相同的方式建模(通过嵌套值),但似乎行不通

推荐答案

可以通过嵌套如下选项来实现:

...
$fieldset->addField('entity', 'select', array(
        'name' => 'action[entity]',
        'title' => 'Entity',
        'label' => 'Entity',
        'required' => true,
        'values' => array(
                    array('value' => array(
                        array('value' => 'one', 'label' => 'One'),
                        array('value' => 'two', 'label' => 'Two')
                    ), 'label' => 'Numbers'))
         ));
...

我错误地嵌套了它:)

Php相关问答推荐

PHP-转义字符串内的双反斜杠

MariaDB 11.3.2+PHP:服务器向客户端发送未知的字符集(0).请向开发人员报告

Laravel:如何展平多维Eager 加载的集合

允许客户定义特定WooCommerce产品的价格

升级到PHP 8.2后无法删除cURL Cookie文件

在WooCommerce中只允许高价产品的BACS付款

Laravel 10中的Validator类

为什么WooCommerce的动作钩子';woocommerce_checkout_update_order_review';包含旧信息?

如何使用PHP从TXM(XML)标记中读取特定属性

允许在WooCommerce管理订单列表中使用计费邮箱进行搜索

在 WooCommerce 中何时何地执行 woocommerce_order_status_completed_notification 挂钩?

单个产品页面 WooCommerce 订阅价格字符串的更改

php/html OnSubmit 禁用输入类型提交未给出预期结果

如何修复正则表达式以在换行符后查找非数值

php preg_match 验证 Tiktok 用户名

PHP header() 是否缓存重定向,如果是,如何防止它这样做?

laravel 中的 OrderBy json 列

如何将数字文本文件添加到 PHP 数学方程式中?

Laravel 自定义中间件:ERR_TOO_MANY_REDIRECTS

多排序子数组 - 包含范围数据的子数组