我在cakePHP中设置了一个用户注册表单,使用inputDefaults
来匹配horizontal forms的推特引导要求
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'));
不幸的是,这会覆盖我的默认设置。如何在不重新定义所有输入元素的所有设置的情况下,一次使用默认设置和自定义标签?
答案 0 :(得分: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)));
你基本上会“覆盖”但仍保持默认选项。
答案 1 :(得分:0)
您可以按照以下步骤进行操作 -
echo $this->Form->input('username', array('label' => array('class' => 'col-sm-2 control-label', 'text' => 'Benutzername'));