[英]CakePHP - staying on the same page
我有一个简单的 controller 用于在表中添加一行,但如果它失败,我想留在同一页面上,但只有 flash 警告消息。
class WarehouseAddressesController{
public function itemToAdd($id = null){
$exists = $this->ItemsAddress->find('first', array('conditions' => array(
'warehouse_addresses_id' => $this->request->data['WarehouseAddress']['id'],
'items_id' => $this->request->data['Items']['itemToAdd'])));
if($this->request->is(array('post')) && empty($exists)){
$data = array(
'warehouse_addresses_id' => $this->request->data['WarehouseAddress']['id'],
'items_id' => $this->request->data['Items']['itemToAdd']
);
if ($this->ItemsAddress->save($data, array('validate' => 'first',))) {
$this->Flash->success(__('This item has been successfully added to the requested address.'));
$this->redirect($this->referer());
}
if(!empty($exists)){
$this->Flash->warning(__('This item already exists on this address'));
$this->redirect($this->referer());
}
else {
$this->Flash->error(__('This item could not be added to this address. Please, try again.'));
$this->redirect($this->referer());
}}}}
保存项目时,它会像使用 $this->referer 一样停留在同一页面上,但是当项目存在时,它会显示错误错误:未找到 WarehouseAddressesController::itemToAdd() 的视图。 但我想留在页面上,只显示页面存在的错误。
我只需要添加 return $this->redirect($this->referer()); 在第一个 if 块和最后一个 if 块中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.