使用CakePHP FormHelper与Bootstrap Forms [英] Using CakePHP FormHelper with Bootstrap Forms

查看:216
本文介绍了使用CakePHP FormHelper与Bootstrap Forms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CakePHP的FormHelper 是在制作CakePHP应用程序时生成表单的方式。可以假设,这包括生成输入元素,如下:

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

这将产生类似这样的HTML:

 < div class =input text> 
< label for =ModelAbc> Abc< / label>
< input name =data [Model] [Abc]class =maxlength =250type =textid =ModelAbc>
< / div>不幸的是,Bootstrap想要类似下面的东西:

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

如何使CakePHP产生这个输出?

解决方案

由lericson的回答启发,这是我的最终解决方案CakePHP 2.x:

 <?php echo $ this-> Form-> create('ModelName',array(
'class'=>'form-horizo​​ntal',
'inputDefaults'=> ; array(
'format'=> array('before','label','between','input','error','after'),
'div'=>
'label'=> array('class'=>'control-label'),
' '< 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') ,// Form-> create()中的预设不适用于我
)); ?>
< / fieldset>
<?php echo $ this-> Form-> end();?>

其中产生:

 < form ...> 
< fieldset>
< div class =control-group required error>
< label for =Fieldnameclass =control-label> Fieldname< / label>
< div class =controls>
< input name =data [Fieldname]class =form-errormaxlength =255type =textvalue =id =Fieldname/>
< span class =help-inline>错误讯息< / span>
< / div>
< / div>
< / fieldset>
< / form>



我基本上添加了'format'和'error'键,并添加了control-标签元素。


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.

这篇关于使用CakePHP FormHelper与Bootstrap Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆