2012-02-29 261 views
27

CakePHP's FormHelper是您在製作CakePHP應用程序時如何生成表單。正如人們可能會認爲,這包括生成輸入元素,就像這樣:將CakePHP FormHelper與Bootstrap Forms一起使用

$this->Form->input('abc'); 

將產生HTML是這樣的:

<div class="input text"> 
    <label for="ModelAbc">Abc</label> 
    <input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc"> 
</div> 

現在,可悲的是,引導要像下面這樣:

<div class="control-group"> 
    <label for="ModelAbc" class="control-label">Abc</label> 
    <div class="controls"> 
    <input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc"> 
    </div> 
</div> 

如何讓CakePHP生成此輸出?

+3

有幫助類可用於這種問題,您可以擴展和進一步改善,如果需要:https://github.com/slywalker/cakephp-plugin-boost_cake – 2013-09-02 09:39:41

回答

11

這裏有一種方法:

<?php echo $this->Form->create(null, array(
    'inputDefaults' => array(
     'div' => array('class' => 'control-group'), 
     'label' => array('class' => 'control-label'), 
     'between' => '<div class="controls">', 
     'after' => '</div>', 
     'class' => '') 
)); ?> 
2

你的答案是正確的,但對其他用戶的利益,還有一些其他的東東可以做採取錯誤/幫助文本的優勢:

添加form-horizontalForm->create()更緊湊的形式(輸入左邊的標籤,而不是在頂部)

下面是如何把幫助文本字段下方class(必須爲每個字段做),不忘CLO選擇</div>

echo $this->Form->input('field_name', array(
      'after'=>'<span class="help-block">This text appears 
       underneath the input.</span></div>')); 

,並正確顯示的錯誤:

// cake 2.0 
echo $this->Form->input('abc', array(
    'error' => array('attributes' => array('class' => 'controls help-block')) 
)); 

輸出:

<div class="control-group required error"> 
    <label for="ModelAbc" class="control-label">Abc</label> 
    <div class="controls"> 
    <input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc"> 
    </div> 
    <!-- error message --> 
    <div class="controls help-block">This is the error validation message.</div> 
    <!-- error message --> 
</div> 

這是額外的加價,而不是整齊的引導,但它是一個快速解決方案。另一種方法是單獨執行每個錯誤消息。

它排隊很好。然而,我還沒有發現一個簡單的方法來使用inline消息。

+0

不能''錯誤'=>數組('attributes'=> array('class'=>'controls help-block'))'代碼進入'inputDefaults'?從你的例子來看,你似乎暗示它必須爲每個領域完成。 – Nick 2012-05-24 16:22:28

32

通過lericson的回答啓發,這是我的CakePHP 2.x的最終解決方案:

<?php echo $this->Form->create('ModelName', array(
    'class' => 'form-horizontal', 
    'inputDefaults' => array(
     'format' => array('before', 'label', 'between', 'input', 'error', 'after'), 
     'div' => array('class' => 'control-group'), 
     'label' => array('class' => 'control-label'), 
     'between' => '<div class="controls">', 
     'after' => '</div>', 
     'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')), 
    )));?> 
<fieldset> 
<?php echo $this->Form->input('Fieldname', array(
    'label' => array('class' => 'control-label'), // the preset in Form->create() doesn't work for me 
    )); ?> 
</fieldset> 
<?php echo $this->Form->end();?> 

主要生產:

<form...> 
<fieldset> 
<div class="control-group required error"> 
    <label for="Fieldname" class="control-label">Fieldname</label> 
    <div class="controls"> 
     <input name="data[Fieldname]" class="form-error" maxlength="255" type="text" value="" id="Fieldname"/> 
     <span class="help-inline">Error message</span> 
    </div> 
</div> 
</fieldset> 
</form> 

我基本上都添加了 '格式' 和 '錯誤' 鍵,並將控件標籤類添加到標籤元素。

+0

太棒了!這正是我所期待的。非常感謝@domsom – Sid 2012-08-17 13:54:46

+0

我想添加 data-bv-field =「password」> 012-=「不同」data -bv-validator-for =「密碼」class =「help-block」>密碼不能與用戶輸入錯誤時的用戶名相同。如何實現這一目標? – 2014-09-14 20:42:12

+0

@RNKushwaha不知道我的問題是否正確,但如果您想針對特定情況產生自定義錯誤消息,請爲此添加自定義驗證程序。如果你的問題是關於產生一個特定的/修改過的標記,我建議你將它作爲一個新問題發佈並將其鏈接到這個問題。 – domsom 2014-09-15 08:25:00

1

小添加另一種意見:

如果whant添加類和更改標籤基地的文字,你可以寫下一

<?php echo $this->Form->input('Fieldname', array(
    'label' => array('class' => 'control-label','text'=>'HERE YOU LABEL TEXT') 
)); ?> 
2

運用同樣的原則之上的形式 - >端功能如下:

<?php echo $this->Form->end(array(
    'label' => __('Submit'), 
    'class' => 'btn', 
    'div' => array(
     'class' => 'control-group', 
     ), 
    'before' => '<div class="controls">', 
    'after' => '</div>' 
));?> 

爲了產生:

<div class="control-group"> 
    <div class="controls"> 
    <input class="btn" type="submit" value="Submit"> 
    </div> 
</div> 
18

下面是引導3

<?php 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'), 
    'class' => array('form-control'), 
    'label' => array('class' => 'col-lg-2 control-label'), 
    'between' => '<div class="col-lg-3">', 
    'after' => '</div>', 
    'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')), 
))); ?> 
<fieldset> 
    <legend><?php echo __('Username and password'); ?></legend> 
    <?php echo $this->Form->input('username'); ?> 
    <?php echo $this->Form->input('password'); ?> 
</fieldset> 
<?php echo $this->Form->end(__('Login')); ?> 

的解決方案如果一個字段需要自己的標籤:

<?php echo $this->Form->input('username', array('label' => array('text' => 'Your username', 'class' => 'col-lg-2 control-label'))); ?> 
+0

你把標籤貼在哪裏?很好的答案。過去幾天一直困惑,試圖弄清楚爲什麼Bootstrap佈局在CakePHP中不起作用。 – 2014-04-03 03:02:49

+0

我可以證實這也適用於蛋糕1.3,謝謝! – 2015-01-14 11:37:57

+0

它像一個魅力(我正在使用CakePHP 2.4。10) – 2015-04-07 02:10:26

1

我使用slywalker/CakePHP的 - 插件 - boost_cake有同樣的問題,我開出罰單他有它在幾個小時內,他更新到1.03修復,並告訴我這樣使用它

<?php echo $this->Form->input('email', array(
    'label' => array(
     'text' => __('Email:'), 
    ), 
    'beforeInput' => '<div class="input-append">', 
    'afterInput' => '<span class="add-on"><i class="icon-envelope"></i></span></div>' 
)); ?> 

我希望它可以幫助另外一個人太

1

爲了得到它在與bootswatch引導水平的形式工作,我不得不使用:

echo $this->Form->create(
    'User', 
    array(
     'action'  => 'add', 
     'admin'   => 'false', 
     'class'   => 'form-horizontal', 
     'inputDefaults' => array(
      'format' => array('before', 'label', 'between', 
           'input', 'error', 'after'), 
      'class' => 'form-control', 
      'div'  => array('class' => 'form-group'), 
      'label' => array('class' => 'col-lg-2 control-label'), 
      'between' => '<div class="col-lg-10">', 
      'after' => '</div>', 
      'error' => array('attributes' => array('wrap' => 'span', 
                 'class' => 'text-danger')), 
     ) 
    ) 
); 

然後,你可以使用它作爲正常:

echo $this->Form->input('User.username'); 
相關問題