2014-02-12 52 views
4

我設置了一個用戶註冊形式在CakePHP中,使用inputDefaults以匹配horizontal formsCakePHP的表單輸入標籤覆蓋inputDefaults

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')), 
      ))); 

在Twitter的引導的要求,我使用

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

來顯示錶單元素。

我想有自定義標籤,就像這樣:

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

不幸的是這將覆蓋我的默認設置。如何一次使用默認設置和自定義標籤,而不重新定義全部設置爲全部輸入元素?

回答

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