Cakephp 3:同一模型每页多个表单
Cakephp 3 : Multiple Forms per page for the same model
我有一个页面,其中有多个表单是使用 FormHelper 生成的,旨在修改同一个实体。问题是:两种形式都会出现验证错误。
在 cakephp 2 中,这个问题通过扩展模型解决了(参见:http://bakery.cakephp.org/articles/RabidFire/2010/06/26/multiple-forms-per-page-for-the-same-model)。
但是我不知道如何用 cakephp 3 做到这一点。
编辑:我将更准确地描述我正在尝试做的事情。
我在同一个页面上有两个表格。第一个使用户能够更改他的电子邮件地址,另一个使用户能够更改他的密码。
这两个表单都是使用表单助手和相同的用户实体创建的。
在这两种形式中,都有一个字段供用户输入其当前密码(作为安全措施)。在允许更改电子邮件或密码之前,验证器将检查输入的密码是否正确。
问题:假设用户尝试更改他的电子邮件但输入了错误的密码,"wrong password" 消息将出现在两个表单上。
这是一种极端情况,FormHelper 还没有准备好妥善处理。但这是一个解决方案,您将需要 2 个实体:
$user = $this->Users->get($id);
$user->unsetProperty('password');
$clonedUser = clone $user;
$this->set(compact('user', 'clonedUser'));
在您看来,您构建表单的方式可以检测您应该传递哪个实体:
echo $this->Form->create($this->request->data('_form1') ? $user : $clonedUser);
... fields here
echo $this->Form->hidden('_form1', ['value' => 1]);
echo $this->Form->create($this->request->data('_form2') ? $user : $clonedUser);
... fields here
echo $this->Form->hidden('_form2', ['value' => 1]);
上面的代码所做的是检测之前提交了哪些表单,并使用空的克隆实体或有错误的实体呈现表单。
偶然发现这个是因为我有同样的要求。我会选择何塞,但将逻辑移至控制器:
$callback = $this->Inquiries->newEntity();
$inquiry = $this->Inquiries->newEntity();
if ($this->request->is('post')) {
if ($this->request->data('_type') === 'callback') {
$callback = $this->Inquiries->patchEntity($callback, $this->request->data, ['validate' => 'callback']);
$entity = &$callback;
} elseif ($this->request->data('_type') === 'inquiry') {
$inquiry = $this->Inquiries->patchEntity($inquiry, $this->request->data);
$entity = &$inquiry;
}
if (!$entity->errors()) {
// do stuff here
}
}
$this->set(compact('callback', 'inquiry'));
传递表单类型:
echo $this->Form->input('_type', ['type' => 'hidden', 'value' => 'inquiry']);
我有一个页面,其中有多个表单是使用 FormHelper 生成的,旨在修改同一个实体。问题是:两种形式都会出现验证错误。
在 cakephp 2 中,这个问题通过扩展模型解决了(参见:http://bakery.cakephp.org/articles/RabidFire/2010/06/26/multiple-forms-per-page-for-the-same-model)。
但是我不知道如何用 cakephp 3 做到这一点。
编辑:我将更准确地描述我正在尝试做的事情。
我在同一个页面上有两个表格。第一个使用户能够更改他的电子邮件地址,另一个使用户能够更改他的密码。
这两个表单都是使用表单助手和相同的用户实体创建的。
在这两种形式中,都有一个字段供用户输入其当前密码(作为安全措施)。在允许更改电子邮件或密码之前,验证器将检查输入的密码是否正确。
问题:假设用户尝试更改他的电子邮件但输入了错误的密码,"wrong password" 消息将出现在两个表单上。
这是一种极端情况,FormHelper 还没有准备好妥善处理。但这是一个解决方案,您将需要 2 个实体:
$user = $this->Users->get($id);
$user->unsetProperty('password');
$clonedUser = clone $user;
$this->set(compact('user', 'clonedUser'));
在您看来,您构建表单的方式可以检测您应该传递哪个实体:
echo $this->Form->create($this->request->data('_form1') ? $user : $clonedUser);
... fields here
echo $this->Form->hidden('_form1', ['value' => 1]);
echo $this->Form->create($this->request->data('_form2') ? $user : $clonedUser);
... fields here
echo $this->Form->hidden('_form2', ['value' => 1]);
上面的代码所做的是检测之前提交了哪些表单,并使用空的克隆实体或有错误的实体呈现表单。
偶然发现这个是因为我有同样的要求。我会选择何塞,但将逻辑移至控制器:
$callback = $this->Inquiries->newEntity();
$inquiry = $this->Inquiries->newEntity();
if ($this->request->is('post')) {
if ($this->request->data('_type') === 'callback') {
$callback = $this->Inquiries->patchEntity($callback, $this->request->data, ['validate' => 'callback']);
$entity = &$callback;
} elseif ($this->request->data('_type') === 'inquiry') {
$inquiry = $this->Inquiries->patchEntity($inquiry, $this->request->data);
$entity = &$inquiry;
}
if (!$entity->errors()) {
// do stuff here
}
}
$this->set(compact('callback', 'inquiry'));
传递表单类型:
echo $this->Form->input('_type', ['type' => 'hidden', 'value' => 'inquiry']);