symfonysymfony6

Symfony 6 custom form error message being ignored


I have the following dropdown widget in my Symfony 6.3 form class:

->add('color', ChoiceType::class, [
  'required' => true,
  'choices' => $choices,
  'constraints' => [
    new Choice([
        'choices' => array_keys($choices),
        'message' => 'Invalid'
    ])
  ]
])

When I try to submit an invalid value for the field, the form ignores my custom error message and instead outputs "The selected choice is invalid." which seems to be a Symfony default message. I'm not using translations.

How can I get this use my custom error message?


Solution

  • Managed to make it work like this:

    ->add('color', ChoiceType::class, [
      'required' => true,
      'choices' => $choices,
      'invalid_message' => 'Invalid',  // message is defined here
      'constraints' => [
        new Choice([
            'choices' => array_keys($choices)
        ])
      ]
    ])