CakePHP 的 FormHelper 是您在制作 CakePHP 应用程序时生成表单的方式.正如人们可能假设的那样,这包括生成输入元素,如下所示:
$this->Form->input('abc');这将产生这样的 HTML:
<label for="ModelAbc">Abc</label><input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">遗憾的是,Bootstrap 需要类似以下内容:
<label for="ModelAbc" class="control-label">Abc</label><div class="控件"><input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">我如何让 CakePHP 产生这个输出?
受 lericson 的回答启发,这是我对 CakePHP 2.x 的最终解决方案:
Form->create('ModelName', array('类' =>'形式水平','inputDefaults' =>大批('格式' =>array('before', 'label', 'between', 'input', 'error', 'after'),'div' =>数组('类' => '控制组'),'标签' =>数组('类' => '控制标签'),'之间' =>'','之后' =>'</div>','错误' =>数组('属性' => 数组('包裹' => '跨度','类' => '帮助内联')),)));?><字段集><?php echo $this->Form->input('Fieldname', array('标签' =>array('class' => 'control-label'),//Form->create() 中的预设对我不起作用));?></fieldset><?php echo $this->Form->end();?>产生:
<字段集><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">错误信息</span>
</fieldset></表单>
我基本上添加了 'format' 和 'error' 键,并将 control-label 类添加到标签元素中.
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.
这篇关于在 Bootstrap 表单中使用 CakePHP FormHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽