【发布时间】:2016-06-10 16:23:25
【问题描述】:
我正在查看与任何模型无关的基本联系表单。对于这种情况,我想要一些关于利用 Cake 的字段错误自动视图呈现的最佳方法的建议。
控制器
通过自定义验证器执行验证。
public function index()
{
if ($this->request->is('post')) {
// Validate the form
$validator = new EnquiryValidator();
$data = $this->request->data();
$errors = $validator->errors($data);
if (empty($errors)) {
// Send email, etc.
// ...
// Refresh page on success
}
// Show error
$this->Flash->error('Unable to send email');
}
}
查看
<?= $this->Form->create(); ?>
<?= $this->Form->input('name', [
'autofocus' => 'autofocus',
'placeholder' => 'Your name',
'required'
]);
?>
<?= $this->Form->input('email', [
'placeholder' => 'Your email address',
'required'
]);
?>
<?= $this->Form->input('subject', [
'placeholder' => 'What would you like to discuss?',
'required'
]);
?>
<?= $this->Form->input('message', [
'label' => 'Query',
'placeholder' => 'How can we help?',
'cols' => '30',
'rows' => '10',
'required'
]);
?>
<div class="text-right">
<?= $this->Form->button('Send'); ?>
</div>
<?= $this->Form->end(); ?>
目前表单不会在输入字段旁边显示任何错误。我认为这是因为没有实体与表单或类似的东西相关联,但我不确定。
什么是最好的解决方案?是否可以以更好的方式执行验证以在视图中自动提供字段错误?
【问题讨论】:
标签: php cakephp cakephp-3.0