通过重定向将表单错误发送回视图

时间:2018-05-16 14:02:04

标签: cakephp

我最近开始接受CakePHP,现在我正在尝试为我的网站建立联系表单。
它似乎可以用来验证表格,但现在我想做点什么 我希望它显示发生的任何错误(比如缺少必填字段)。 但是,我希望它显示两种不同的可能状态:
- 用户错误(用户忘记了字段或字段不符合要求),在字段下方显示(使用Bootstrap 4)
- 服务器错误(邮件无法从服务器发送 - 例如,SMTP服务器已关闭),使用Flash

显示

我一直在为此广泛搜索,但如果不自己发布问题,我就无法进一步了解这一点。
下面是我使用的所有代码(运行CakePHP 3.6)

src / Template / Pages / contact.ctp(由PagesController控制):

<div class="container">
  <div class="row no-banner">
      <div class="col-md-6">
        <h4>Contact Form</h4>
        <div style="padding-bottom:25px;">
          Got a question? we'd love to hear it from you!<br />
          Send us a message and we'll respond as soon as possible!
        </div>

        <?= $this->Flash->render(); ?>

        <?= $this->Form->create("Contact",array("url"=>"/contact","class"=>"contact-form","id"=>"contact-form")); ?>
        <?= $this->Form->control("name",array("placeholder"=>"Your Name","label"=>false,"class"=>"form-control")); ?>
        <?= $this->Form->control("email",array("placeholder"=>"Your Email","label"=>false,"class"=>"form-control")); ?>
        <?= $this->Form->control("subject",array("placeholder"=>"The Subject","label"=>false,"class"=>"form-control")); ?>
        <?= $this->Form->textarea("message",array("placeholder"=>"Your Message","label"=>false,"class"=>"form-control")); ?>
        <?= $this->Form->button('Submit',array("class"=>"btn")); ?>
        <?= $this->Form->end(); ?>
      </div>
      <div class="col-md-6">
        <h4>Social Media</h4>
        <div style="padding-bottom:25px;">
          We are active on a variety of of social media, feel free to like and follow us!
        </div>
        <a href="#"><i class="fab fa-facebook social-media-icon"></i></a>
        <a href="#"><i class="fab fa-discord social-media-icon"></i></a>
        <?= $this->Form->errors; ?>
      </div>
  </div>
</div>

SRC /控制器/ ContactController.php:

<?php
  namespace App\Controller;

  use App\Controller\AppController;
  use App\Form\ContactForm;

  class ContactController extends AppController {
    public function add() {
      $contact = new ContactForm();
      if ($this->request->is('post')) {
        if ($contact->execute($this->request->getData())) {
          $this->Flash->success('We will get back to you asap!');
          $this->redirect($this->referer());
        } else {
          $this->Flash->error('There was an issue sending your mail. Please try again later!');
          $this->redirect($this->referer());
        }
      }
      $this->set('contact', $contact);
    }
  }

SRC /形式/ ContactForm.php:

<?php
  namespace App\Controller;

  use App\Controller\AppController;
  use App\Form\ContactForm;

  class ContactController extends AppController {
    public function add() {
      $contact = new ContactForm();
      if ($this->request->is('post')) {
        if ($contact->execute($this->request->getData())) {
          $this->Flash->success('We will get back to you asap!');
          $this->redirect(array('controller' => 'Pages','action' => 'display','contact'));
        } else {
          $this->Flash->error('There was an issue sending your mail. Please try again later!');
          $this->redirect($this->referer());
        }
      }
      $this->set('contact', $contact);
    }
  }

1 个答案:

答案 0 :(得分:2)

请勿重定向,因为您不需要。而不是使用Pages控制器来显示表单,而是将表单放在控制器视图中:"scripts": { "tslint": "tslint -p tsconfig.json", "tslint-fix": "tslint --fix -p tsconfig.json" } 。无需再拨打/Contacts/add.ctp

错误已经添加到表单中,表单助手会找到它们,因为您将表单对象传递给Form-&gt; create()。

如果你想让它更复杂,坚持你的实现,只需将错误写入会话,并在接收它的动作中从会话中读取它们,并通过redirect()将它们设置为表单对象