【发布时间】:2015-05-01 06:52:01
【问题描述】:
我根据文档构建表单模板。在我收到字段错误之前,似乎一切都很好。现在我有两个问题:
- 出现错误时如何更改表单字段的类名?
解决方案:$this->loadHelper('Form', [
'templates' => 'your_template_file',
'errorClass' => 'your-class',
]);
- 当字段出错时,如何在 cakephp 的错误消息中设置
escape => false?因为我在那个 div 中有图标,比如
<div class="error-message"><i class="fa fa-times"></i> My error</div>
嗯,我得到了解决方案的一部分。为了转义 HTML,我可以在所有字段中输入 $this->Form->error('field', null, ['escape' => false]);,但这是一项艰巨的手动任务。我想保留所有字段错误的默认值。我可以编辑 FormHelper.php 类。但是,我认为这不是一个好主意。
我的表单模板是:
'formStart' => '<form {{attrs}} class="form-horizontal" novalidate>',
'inputContainer' => '{{content}}',
'input' => '<input type="{{type}}" name="{{name}}" {{attrs}} class="form-control"/>',
'checkbox' => '<input type="checkbox" value="{{value}}" name="{{name}}" {{attrs}}/>',
'textareaContainerError' => '{{content}}',
'textarea' => '<textarea name="{{name}}" {{attrs}} class="form-control"></textarea>',
'select' => '<select name="{{name}}" {{attrs}} class="form-control">{{content}}</select>',
'button' => '<button {{attrs}} class="btn btn-primary">{{text}}</button>',
'nestingLabel' => '{{input}}',
'formGroup' => '{{input}}',
【问题讨论】:
-
更改哪些字段的类名确切?
input、textarea等实际输入字段?为什么要删除将具有适当的error类集的错误容器?您是否也删除了其他人,而不仅仅是此处显示的 textareas ? -
我可以在所有表单字段出错时更改它们的类名。我将 errorClass 选项放在 loadHelper 中: $this->loadHelper('Form', [ 'templates' => 'admin_form_template', 'errorClass' => 'another-class', ]);但是,第 2 期,我还不能。我需要为 cakephp 设置 escape => false 不会转义 html 标签...
标签: forms cakephp cakephp-3.0