Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2: my form returns false from isValid() but empty array for getErrors() from unique constraint condition
    primarykey
    data
    text
    <p>I have a Customer entity that only has a unique Email field to it. I am trying to edit a customer's email and the validation works fine. However I have this in my controller:</p> <pre><code>public function updateAction(Request $request, $id) { $em = $this-&gt;getDoctrine()-&gt;getManager(); $entity = $em-&gt;getRepository('AcmeDemoBundle:Customer')-&gt;find($id); if (!$entity) { throw $this-&gt;createNotFoundException('Unable to find Customer entity.'); } $editForm = $this-&gt;createForm(new CustomerType(), $entity); $editForm-&gt;bind($request); if ($editForm-&gt;isValid()) { $em-&gt;persist($entity); $em-&gt;flush(); return $this-&gt;redirect($this-&gt;generateUrl('ticket_result')); } var_dump($editForm-&gt;getErrors()); return $this-&gt;render('AcmeDemoBundle:Customer:edit.html.twig', array( 'entity' =&gt; $entity, 'edit_form' =&gt; $editForm-&gt;createView(), )); } </code></pre> <p>The var_dump returns an empty array but the validator sets a unique error and the <code>$editForm-&gt;isValid()</code> returns false. Is there a way to check for that specific error in the controller during validation, also can you explain why it returns an empty error array? Basically, I would like to provide the "merge" option if that error comes up.</p> <p><b>EDIT</b>: here is the formtype:</p> <pre><code>namespace Acme\DemoBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class CustomerType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder -&gt;add('email', 'email', array('required'=&gt;true)) ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver-&gt;setDefaults(array( 'data_class' =&gt; 'Acme\DemoBundle\Entity\Customer', 'cascade_validation' =&gt; true, )); } public function getName() { return 'acme_demobundle_customertype'; } } </code></pre> <p>And the twig template:</p> <pre><code>{% extends 'AcmeDemoBundle::layout.html.twig' %} {% block body -%} &lt;h1&gt;Customer edit&lt;/h1&gt; &lt;form action="{{ path('customer_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}&gt; &lt;input type="hidden" name="_method" value="PUT" /&gt; {{ form_widget(edit_form) }} &lt;p&gt; &lt;button type="submit"&gt;Edit&lt;/button&gt; &lt;/p&gt; &lt;/form&gt; {% endblock %} </code></pre> <p>Here is my validation:</p> <pre><code>Acme\DemoBundle\Entity\Customer: constraints: - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: fields: email message: "A customer under that email address already exists" properties: email: - Email: ~ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload