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

Cakephp 3.0 change or remove wrapping div on input form

我正在尝试删除或更改 CakePHP 在其表单助手上使用的包装 div。

当我使用这段代码时:

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

输出为:

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

而我想要的是:

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

我曾经在 CakePHP 2 上这样做,向输入法添加更多选项,但是在最新的 CakePHP 版本上这不起作用。有什么线索吗?

谢谢

使用 FormHelper 模板

要更改表单中 所有输入 的换行,请使用:

$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
]);

要更改 单输入 的换行,请使用:

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

有关模板的完整参考,请阅读:Customizing the Templates FormHelper Uses

版本 3 不再支持自定义包装的 CakePHP 2 样式。来自迁移指南:

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.

我正在使用购买的 UI,cakephp3 出现了几个问题。对我来说,删除 <div> 首字母并不是那么容易,大多数是这里提供的解决方案,经过多次测试:

echo $this->Form->control('username', [
  'templates'     => ['inputContainer' => '{{content}}'],
  "type"          => "text",
  "aria-invalid"  => "false",
  "aria-required" => "true",
  "class"         => "form-control valid",
  "placeholder"   => "Ingrese su usuario o email ...",
  "autocomplete"  => "on",
  'label'         => false
]);

结果

<input name="username" aria-invalid="false" aria-required="true" class="form-control valid" placeholder="Ingrese su usuario o email ..." autocomplete="on" id="username" type="text">

只添加了一个输入标签(对不起我的Google-英文)

我认为在配置文件夹中定义全局模板是更好的方法:

<?= $this->Form->create($user, array(
    "class" => "ui form", 
    "templates" => "semantic" // The filename in your config folder without .php
)); ?>

在 config 文件夹中创建文件 "semantic.php"(您可以随意命名)内容为:

return array(
    "inputContainer" => '{{content}}' // Here the magic happens
);

希望对您有所帮助!