1

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

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks! As i thought this is the only one solution :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.