0

通常在 cakephp2 上,我曾经取消设置表单数据,一切都很好。

有时我使用重定向来清除它。但我不能在当前页面中这样做。

任何人都发现了这个问题或解决方案?

4

2 回答 2

2

如果您在成功添加记录后停留在“添加”页面,例如允许更快地输入多条记录,则需要在保存后重置实体。例如,如果您正在输入帖子,您的控制器将如下所示:

$post = $this->Posts->newEntity();
if ($this->request->is('post')) {
    $post = $this->Posts->patchEntity($post, $this->request->data);
    if ($this->Posts->save($post)) {
        $post = $this->Posts->newEntity(); // <- Reset the entity
    }
}
$this->set(compact('post'));

(为简洁起见,错误检查、闪现消息等都省略了。)

于 2015-12-24T03:41:20.377 回答
0

另一种方法是简单地重定向到同一页面。我的问题是并非所有内容都被删除

$contact = new ContactForm();
    if ($this->request->is('post')) {
        if ($contact->execute($this->request->data)) {
            $this->Flash->success(__('Submited.'));
            $this->request->data(null);
            $contact = new ContactForm();
            return $this->redirect('/(Same Page)');// This did the final trick for me
        } 
    }
于 2017-02-13T09:29:39.027 回答