4

我在 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'));

不幸的是,这会覆盖我的默认设置。如何在不重新定义所有输入元素的所有设置的情况下同时使用默认设置和自定义标签?

4

2 回答 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)));

您将基本上“覆盖”但仍保持默认选项。

于 2014-02-12T23:32:16.877 回答
0

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

echo $this->Form->input('username', array('label' => array('class' => 'col-sm-2 control-label', 'text' => 'Benutzername'));
于 2015-06-16T02:47:50.027 回答