misterlewy1 misterlewy1 - 1 month ago 11
Twig Question

Symfony display form errors of form created on rendered controller

I'm rendering controller 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(),
));

}


Inside twig template

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


End everythink is all right, the form shows and inserts data into database.
BUT if the form is not valid, user is redirected to route /email, what should I do to display form errors on rendered view? Thank you :D

Here's my Email entity:

<?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;
}
}

Answer

As here is stated, all you need to do is to check, in your action method, for validation rules you've set in the Entity class.

Something like:

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

Then, if there are validation errors, just send them to the desired twig template:

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

And finally, to display the 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>