cakePHP 表单输入标签覆盖 inputDefaults

问题描述 投票:0回答:2

我正在 cakePHP 中设置用户注册表单,使用

inputDefaults
来匹配 水平表单的 twitter 引导程序要求

    echo $this->Form->create('User', array(
            'class' => 'form-horizontal',
            'role' => 'form',
            'inputDefaults' => array(
                'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
                'div' => array('class' => 'form-group'),
                'label' => array('class' => 'col-sm-2 control-label'),
                'between' => '<div class="col-sm-10">',
                'after' => '</div>',
                'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
            )));

里面,我正在使用

echo $this->Form->input('username');

显示表单元素。

我想要自定义标签,如下所示:

echo $this->Form->input('username', array('label' => 'Benutzername'));

不幸的是,这会覆盖我的默认设置。如何立即使用默认设置和自定义标签,而不重新定义 all 输入元素的 all 设置?

php input cakephp default form-helpers
2个回答
4
投票

我会这样做

$mainLabelOptions = array('class' => 'col-sm-2 control-label');
echo $this->Form->create('User', array(
    'class' => 'form-horizontal',
    'role' => 'form',
    'inputDefaults' => array(
        'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
        'div' => array('class' => 'form-group'),
        'label' => $mainLabelOptions,
        'between' => '<div class="col-sm-10">',
        'after' => '</div>',
        'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
        )));

//then I would create a new label options array and have it merged to the main one
$myLabelOptions = array('text' => 'Benutzername');
echo $this->Form->input('username', array('label' => array_merge($mainLabelOptions, $myLabelOptions)));

您基本上会“覆盖”,但仍保留默认选项。


0
投票

您可以简单地执行以下操作-

echo $this->Form->input('username', array('label' => array('class' => 'col-sm-2 control-label', 'text' => 'Benutzername'));
© www.soinside.com 2019 - 2024. All rights reserved.