如何在 cakephp 3 中闪烁不同的表单操作字段验证错误消息
How to flash diffrent form action field validation error message in cakephp 3
我有一个视图,其中有 2 个表单。一个用于登录,另一个用于注册。对于注册操作,我使用了操作
Users/add
我已经在我的 login.ctp
中给出了表单操作,比如
<?= $this->Form->create($user, ['url' => ['action' => 'add']]); ?>
在添加动作中我写了代码
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'login']);
}else{
$this->Flash->error(__('The user could not be saved. Please, try again.'));
return $this->redirect(['action' => 'login']);
}
}
$this->set(compact('user'));
}
现在,如果我出现任何验证错误,例如密码匹配验证错误,该错误不会显示在字段 password
下。但是我可以从 add.ctp 中看到它。
如何发送此验证错误消息,从添加操作到登录操作?
如果需要将当前动作转发到同一控制器上的不同动作,可以使用Controller::setAction()
public function add()
{
$this->setAction('login');
}
我有一个视图,其中有 2 个表单。一个用于登录,另一个用于注册。对于注册操作,我使用了操作
Users/add
我已经在我的 login.ctp
中给出了表单操作,比如
<?= $this->Form->create($user, ['url' => ['action' => 'add']]); ?>
在添加动作中我写了代码
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'login']);
}else{
$this->Flash->error(__('The user could not be saved. Please, try again.'));
return $this->redirect(['action' => 'login']);
}
}
$this->set(compact('user'));
}
现在,如果我出现任何验证错误,例如密码匹配验证错误,该错误不会显示在字段 password
下。但是我可以从 add.ctp 中看到它。
如何发送此验证错误消息,从添加操作到登录操作?
如果需要将当前动作转发到同一控制器上的不同动作,可以使用Controller::setAction()
public function add()
{
$this->setAction('login');
}