【发布时间】:2015-12-21 05:41:01
【问题描述】:
我有两个表员工和用户员工之间有字段 user_id 关系我们用户有一个员工和员工属于用户,我的视图文件中有以下代码
<?php
echo $this->Form->input('employee.name');
echo $this->Form->input('user.email');
echo $this->Form->input('user.username');
echo $this->Form->input('employee.employee_level');
?>
我的 EmployeeController 代码是
<?php
$employee = $this->Employees->newEntity($this->request->data);
$user = $this->Employees->Users->newEntity($this->request->data);
if ($this->request->is('post')) {
$employee = $this->Employees->patchEntity($employee, $this->request->data, [
'associated' => ['Users']
]);
if($this->Employees->save($employee)) {
$this->Flash->success(__('The employee has been saved.'));
} else {
$this->Flash->error(__('The employee could not be saved. Please, try again.'));
}
}
?>
如果一切顺利且完美,它会保存数据,但如果存在一些验证错误,例如用户名已经存在或电子邮件已经存在,而不是简单地显示无法保存员工的闪存消息,请重试它不显示模型当我尝试将数据保存在单个表中时,字段下的验证错误消息如附图所示
请告诉我我的视图或控制器文件中的问题是什么,以便如果两个模型中的任何一个出现验证错误而不是在相关字段下显示错误消息
对不起我的英语不好 谢谢
【问题讨论】:
标签: validation cakephp cakephp-3.0 cakephp-model