12

I've noticed that when using the Symfony 2.3 choice field type, if I try to submit an invalid option (by manually changing the value of an option), symfony reports a form error on that field that says "This value is not valid".

However, I see no option to change this message in the choice field type class. I have not validation constraints set up for this field either.

Where is this error message coming from, and how do I change it?

user3009816
  • 797
  • 2
  • 8
  • 16
  • Yoy might need to use form event listeners if you change choice fields browser-side (i.e. AJAX). Please see http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html – Alberto Gaona Nov 19 '13 at 18:28

3 Answers3

18

The sentence "The value is not valid" is the default error message which results of a form field transformation failure. This error message is related to each field in case of a data transformer failed (in your case it seems you try to send an invalid choice value). If you want to override it, you can use the invalid_message & invalid_message_parameters form type options.

I can't find the official documentation of this property right now but you can take a look to this for more details: link

egeloen
  • 5,844
  • 1
  • 27
  • 38
  • 1
    Here's the link to *invalid_message* property doc: https://symfony.com/doc/current/reference/forms/types/form.html#invalid-message It is inherited from the Form Type, since v2.3 – userfuser Jul 12 '16 at 15:29
2

The message is coming from the choice constraint, not the choice field type class.

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

The message option can be overridden with your own message.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
  • 1
    Thanks for your reply, but I have not setup any validation constraints for the entity class so I doubt that it is stemming from that. – user3009816 Nov 19 '13 at 17:32
-1

You can get value of parameter to check is valid after set value valid or null for it.

Get value of field after submit form

   $name = $request->request->get('name');

Check it is not valid after set value valid for it

if (empty($name)) {
  $request->request->set('name', 'your name');
}