Reputation: 1724
I have a registration form, after submit,show flash messages and redirect to specific pages.My flash success messages works fine if no errors found.However, error messages will not show up, when there is an error example blank field, but instead the default Symfony2 exceptions displays.
(DBALexception, PDOexception,SQLexceoption,etc) [2/2] DBALException: An exception occurred while executing 'INSERT INTO voters ( blah and blah......)
This is in development stage,I want to test error messages and I want to display the form again and the error flashes instead of Symfony2 500 error page;
How to temporarily disable Symfony2 exceptions in specific pages and instead show up the error flash messages during development?
I have this in controller
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$this->addFlash('notice', 'Welcome to the growing lists of Supporters, dont forget to share and invite this to your friends and relatives, have a magical day!');
//return $this->redirect($this->generateUrl('vo_show', array('id' => $entity->getId())));
return $this->redirect($this->generateUrl('vo'));
}
else {
$this->addFlash('error', 'Welcome to the Death Star, have a magical day!');
return $this->render('Bundle:Vo:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
In twig
{% if app.session.flashBag.has('notice') %}
<div class="alert alert-success fade in" role="alert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
{% for msg in app.session.flashBag.get('notice') %}
{{ msg }}
{% endfor %}
</div>
{% endif %}
{% if app.session.flashBag.has('error') %}
<div class="alert alert-error fade in" role="alert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
{% for msg in app.session.flashBag.get('error') %}
{{ msg }}
{% endfor %}
</div>
{% endif %}
//registerform.twig.html
{{ form_start(form, {attr: {novalidate: 'novalidate'}} ) }}
{{ form_errors(form) }}
{{ form_row(form.comments,{'attr': {'placeholder': 'Why You Want Death'}}) }}
{{ form_end(form) }}
So instead of showing Symfony2 exceptions, I want to redirect back to the form and show individual form errors if form submitting failed
In Symfony 1.4 , I can easily do it in form.class which extends the base class like this
$this->mergePostValidator(new sfValidatorDoctrineUnique(array(
'model' => 'Voters',
'column' => array('firstname', 'middlename', 'lastname', 'city_id', 'birthday', 'profession_id')),array('invalid' => '<div class="alert alert-warning">You are already registered.No need to proceed buddy</div>')
));
And it will create a flash error message in web page if there is an error when filling the form and redisplay the form instead of redirecting to 501 page.Otherwise it will create a success flash message.I want to do it the same way html5 validation,where every error is rendered in form
Upvotes: 2
Views: 7930
Reputation:
A great way to do this in Twig file is just like this:
{% for message in app.flashes('name_of_flash_message') %}
<div class="alert alert-success"> {# bootstrap class #}
<h3>{{ message }}</h3>
</div>
{% endfor %}
make sure, that you define the flash message in your controller:
e.g.
$this->addFlash('name_of_flash_message', "Your message here");
Upvotes: 0
Reputation:
Create a constraint in YML format
for example
Project\Bundle\YourBundle\Entity\Vo:
properties:
firstname:
- NotBlank: ~
Upvotes: 2
Reputation: 1391
You can't do that. Exceptions 'break' the flow of the program, and you can't just simply continue executing the code when you encounter one.
You can however manually catch exceptions in your code and then display what went wrong like this:
if ($form->isValid()) {
try{
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
} catch(\Exception $e){
$this->addFlash('notice', sprintf('A %s was thrown when trying to persist
the entities with message = %s', get_class($e), $e->getMessage());
}
// Rest of your code ...
// Take note that when you encounter exception the flow of the program
// is 'broken' and you must carefully examine what happened to avoid
// unintended consequences and stuff like null valued variables
Upvotes: 1