Symfony显示在渲染控制器上创建的表单错误

时间:2016-10-14 13:11:37

标签: php forms symfony twig

我正在渲染控制器EmailControoler

 /**
 * @Route("/email", name="email")
 */
function insertEmailAction(Request $request)
{
    $request = $this->get('request_stack')->getMasterRequest();

    $email = new Email();

    $form = $this->createForm(SendEmailType::class, $email);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid())
    {
        $em = $this->getDoctrine()->getManager();
        $em->persist($email);
        $em->flush();

        $referer = $request->headers->get('referer');
        return $this->redirect($referer);

    }
     return $this->render('PTBEmailBundle:Default:index.html.twig', array(
        'form' => $form->CreateView(),
    ));

}

内部树枝模板

{{ render(controller('PTBEmailBundle:Email:insertEmail', {'request':app.request})) }}

结束Everythink是可以的,表单显示并将数据插入数据库。 但是如果表单无效,用户被重定向到路由/电子邮件,我该怎么做才能在渲染视图上显示表单错误?谢谢:D

这是我的电子邮件实体:

    <?php

namespace DEERCMS\EmailBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * Email
 *
 * @ORM\Table(name="email")
 * @ORM\Entity(repositoryClass="DEERCMS\EmailBundle\Repository\EmailRepository")
 */
class Email
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @Assert\Email(message="This is not valid e-mail")
     * @ORM\Column(name="email", type="string", length=255, unique=true)
     */
    private $email;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="addDate", type="datetime")
     */
    private $addDate;


    public function __construct() {
        $this->addDate = new \DateTime;
    }
    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set email
     *
     * @param string $email
     *
     * @return Email
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set addDate
     *
     * @param \DateTime $addDate
     *
     * @return Email
     */
    public function setAddDate($addDate)
    {
        $this->addDate = $addDate;

        return $this;
    }

    /**
     * Get addDate
     *
     * @return \DateTime
     */
    public function getAddDate()
    {
        return $this->addDate;
    }
}

1 个答案:

答案 0 :(得分:3)

当陈述here时,您需要做的就是在您的操作方法中检查您在实体类中设置的验证规则。

类似的东西:

$email = new Email();
// ...
$validator = $this->get('validator');
$errors = $validator->validate($email);

然后,如果存在验证错误,只需将它们发送到所需的树枝模板:

if (count($errors) > 0) {
    return $this->render('default/whatever.html.twig', array(
        'errors' => $errors,
    ));
}

最后,要显示错误:

{# app/Resources/views/default/whatever.html.twig #}
<h3>The email has the following errors</h3>
<ul>
    {% for error in errors %}
        <li>{{ error.message }}</li>
    {% endfor %}
</ul>