Question

I've added the following constraint to my entity:

 * @ORM\Table(name="link", uniqueConstraints={@ORM\UniqueConstraint(name="unique_link_idx", columns={"first_form_id", "first_question_id","last_form_id","last_question_id"})})
 * @UniqueEntity(fields={"firstForm", "firstQuestion","lastForm","lastQuestion"}, message="Cette liaison existe déjà !")
 * @ORM\Entity(repositoryClass="Ineat\LinkQuestionBundle\Entity\LinkRepository")

The UniqueEntity constraint works well because before adding it, when trying to insert the exact same entity to the DB I had a DBALException abount integrity constraint. Since then I havn't this exception anymore, the form isn't validating but {{ form_errors(form) }} doesn't print any message.

After having checked in the controller $form->getErrors() returns an empty array while $form->getErrorsAsString() contains the UniqueENtity constraint.

How can I have my UniqueEntity's constraint error message showing

Was it helpful?

Solution

As the UniqueEntity validation seems to fail in one of your child-forms ...

(as the error message is only present in getErrorsAsString and not in getErrors)

... in order to have the child-form errors available in your parent-form use error-bubbling:

$formBuilder->add('child-form','child-form-type', array(
       'error_bubbling' => true,
   )
)

... or inside your child form:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'error_bubbling' => true,
    ));
}

OTHER TIPS

Sometimes the issue occurs because the entity isn't binding the error message to the right field. Using the validation.yml file gives you more control over how and where the error message should be treated.

# src/Ineat/LinkQuestionBundle/Resources/config/validation.yml
Ineat\LinkQuestionBundle\Entity\LinkRepository:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
            fields: [firstForm, firstQuestion, lastForm, lastQuestion]
            errorPath: lastQuestion
            message: 'This port is already in use on that host.'

More information here : Symfony - UniqueEntity

Also putting text strings into "translation" files is good practice (even if you only use/have one language). You won't have bits of text lying around in your views / entities / forms / controllers ... They will all be in you translation folder, in one location. Duplicate strings can also be changed all at once if necessary. It would look like this :

# src/Ineat/LinkQuestionBundle/Resources/config/validation.yml
Ineat\LinkQuestionBundle\Entity\LinkRepository:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
            fields: [firstForm, firstQuestion, lastForm, lastQuestion]
            errorPath: lastQuestion
            message: 'linkquestion.form.errors.unique'

# src/Ineat/LinkQuestionBundle/Resources/translations/validators.fr.yml
linkquestion:
  form:
    errors:
      unique: "This port is already in use on that host."
# Or which ever structure you choose.

Then just tell your application that it will always be in French.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top