在 Bootstrap 表單中使用 CakePHP FormHelper
2023-03-20
php問題
html5模板網(wǎng)
Using CakePHP FormHelper with Bootstrap Forms(在 Bootstrap 表單中使用 CakePHP FormHelper)
本文介紹了在 Bootstrap 表單中使用 CakePHP FormHelper的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
CakePHP 的 FormHelper 是您在制作 CakePHP 應(yīng)用程序時(shí)生成表單的方式.正如人們可能假設(shè)的那樣,這包括生成輸入元素,如下所示:
$this->Form->input('abc');
這將產(chǎn)生這樣的 HTML:
<label for="ModelAbc">Abc</label><input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
遺憾的是,Bootstrap 需要類似以下內(nèi)容:
<label for="ModelAbc" class="control-label">Abc</label><div class="控件"><input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
我如何讓 CakePHP 產(chǎn)生這個(gè)輸出?
解決方案
受 lericson 的回答啟發(fā),這是我對(duì) CakePHP 2.x 的最終解決方案:
Form->create('ModelName', array('類' =>'形式水平','inputDefaults' =>大批('格式' =>array('before', 'label', 'between', 'input', 'error', 'after'),'div' =>數(shù)組('類' => '控制組'),'標(biāo)簽' =>數(shù)組('類' => '控制標(biāo)簽'),'之間' =>'','之后' =>'</div>','錯(cuò)誤' =>數(shù)組('屬性' => 數(shù)組('包裹' => '跨度','類' => '幫助內(nèi)聯(lián)')),)));?><字段集><?php echo $this->Form->input('Fieldname', array('標(biāo)簽' =>array('class' => 'control-label'),//Form->create() 中的預(yù)設(shè)對(duì)我不起作用));?></fieldset><?php echo $this->Form->end();?>
產(chǎn)生:
<字段集><div class="control-group required error"><label for="Fieldname" class="control-label">Fieldname</label><div class="控件"><input name="data[Fieldname]" class="form-error" maxlength="255" type="text" value="" id="Fieldname"/><span class="help-inline">錯(cuò)誤信息</span>
</fieldset></表單>
我基本上添加了 'format' 和 'error' 鍵,并將 control-label 類添加到標(biāo)簽元素中.
CakePHP's FormHelper is how you generate forms when making CakePHP applications. As one might assume, this includes generating input elements, like so:
$this->Form->input('abc');
Which will produce HTML something like this:
<div class="input text">
<label for="ModelAbc">Abc</label>
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
Now, sadly, Bootstrap wants something like the following:
<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>
How do I make CakePHP produce this output?
解決方案
Inspired by lericson's answer, this is my final solution for 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();?>
Which produces:
<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>
I basically added the 'format' and 'error' keys, and added the control-label class to the label element.
這篇關(guān)于在 Bootstrap 表單中使用 CakePHP FormHelper的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!