且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Cakephp 3.0更改或删除输入表单上的包装div

更新时间:2023-12-05 10:58:28

使用FormHelper模板



要在表单中更改所有输入的包装,请使用:

  $ this-> Form-> templates([
'inputContainer'=>'< div class =myOwnClass> {{content}}< / div>'
]);
//或完全删除
$ this-> Form-> templates([
'inputContainer'=>'{{content}}'
]
//现在得到所需包装的输入
echo $ this-> Form-> input('contact',[
'label'=> false
] ;

要更改单输入的包装:

  echo $ this-> Form-> input('contact',[
'templates'=> [
'inputContainer'=>'< div class =myOwnClass> {{content}}< / div>'
],
'label'=> false
] );

有关模板的完整引用,请阅读: 自订范本FormHelper使用



CakePHP 2自定义封装的样式在版本3中不再受支持。从迁移指南:


,after,between和errorMessage选项已从$($)
中删除。您可以使用模板更新换行
HTML。 templates选项允许您为一个输入覆盖已加载的模板



I am trying to remove or change the wrapping div that CakePHP uses on its form helper.

When I use this code:

 echo $this->Form->input('contact', ['label' => false]);

The output is:

<div class="input text">
  <input type="text" id="contact" maxlength="255" name="contact">
</div>

And what I want is:

<div class="myOwnClass">
  <input type="text" id="contact" maxlength="255" name="contact">
</div>

I used to do that on CakePHP 2 adding more options to the input method, however on the latest CakePHP version this isn't working. Any clues?

Thanks

Use FormHelper Templates

To change wrapping for all inputs in form use:

$this->Form->templates([
    'inputContainer' => '<div class="myOwnClass">{{content}}</div>'
]);
// or remove completely
$this->Form->templates([
    'inputContainer' => '{{content}}'
]);
// now get input with desired wrapping
echo $this->Form->input('contact', [
    'label' => false
]);

To change wrapping for single input use:

echo $this->Form->input('contact', [
    'templates' => [
        'inputContainer' => '<div class="myOwnClass">{{content}}</div>'
    ],
    'label' => false
]);

For complete reference on templates read: Customizing the Templates FormHelper Uses

CakePHP 2 style of customizing the wrappings is not supported anymore in version 3. From migration guide:

The div, before, after, between and errorMessage options have been removed from input(). You can use templates to update the wrapping HTML. The templates option allows you to override the loaded templates for one input.