1
votes

Building a Symfony2 form with choices, there is a option "empty value", to set the text related to the empty choice.

However, I want to set my choice field as mandatory, so the empty value should be displayed but no choosen. Using the empty or null constraints, there is no error when the user send the "" value. How should this be done?

2
How are you setting your choice field as mandatory by the way - are you using the form field attribute required => true? If you are, you should note that this is a flag to enable the HTML5 required attribute. It's not the same as constraint validation: symfony.com/doc/current/reference/forms/types/… - Darragh Enright

2 Answers

2
votes

You can add a NotBlank() validation constraint to the appropriate property in the form's underlying entity.

How you do this depends on whether you are using annotations or yaml for your validation definitions.

There is some information about the validator here:

http://symfony.com/doc/current/reference/constraints/NotBlank.html

Hope this helps :)

0
votes

Sorry for inconvenience, I had a listener that overwrote the constraint I set before for choices only, that's why it didn't work like it should...