4

What is the purpose of the message parameters array in a Symfony form error ?

For example, I have the following case: on a form I have a subscriber in which, based on the information given by the user an API may be called and some additional errors could be added to the Symfony form.

As such, when an error occurs, I add a new error on a field:

$myForm->get('name')->addError(
    new FormError('name.api_invalid', null, array('{{ api_name }}' => $someValue))
);

where 'name.api_invalid' is defined in message.en.yml as

name.api_invalid: "The API says the name is actually {{ api_name }}. Please fix before proceeding."

While the message is translated, the "parameters" are not replaced.

Is this not how form error parameters are supposed to work ?

Note: I can make it work by using

$myForm->get('name')->addError(
    new FormError(
        $this->translator->trans('name.api_invalid', array('{{ api_name }}' => $someValue))
    )
);

but I'm really curious about those error parameters.

Thank you!

2
  • 1
    No idea if this would work off the top of my head, but I recommend replacing {{ api_name }} with %api_name% which better resembles Symfony convention and usage of parameters. The way you are using that translation message looks like a Twig variable when it's not. Commented Feb 8, 2016 at 16:29
  • @JasonRoman Hmm ... I chose {{ api_name }} because this is also the way variables are used in the Symfony constraint messages (symfony.com/doc/current/reference/constraints/Length.html). Tried using % instead, but still no luck, Either way though, in the end this is just a token, so whatever syntax each dev prefers, it should work ... but it doesn't :) Commented Feb 8, 2016 at 20:07

1 Answer 1

4

The behavior you are looking at was changed in Symfony 2.2:

Translating validation errors is now optional. You can still do so manually if you like, or you can simplify your templates to simply output the already translated message.

If you look at the form_errors block in 2.1 vs. the form_errors block in 2.2 you will see the difference in how the errors are displayed. You could override the block and do it the old way and then import that template wherever you need it, or you can simply translate the error message the way you're doing above (which is how I've typically done it and is perfectly acceptable).

If you're going the route of $translator->trans then I would use %..% in the parameters, as stated in the Symfony documentation:

The placeholders can take on any form as the full message is reconstructed using the PHP strtr function. But the %...% form is recommended, to avoid problems when using Twig.

The {{ }} you are using is more relegated to using Symfony Validators and how they build their violations (example here).

Now, if you want your messages and parameters automatically translated without manually throwing a FormError, then I would simply suggest creating a custom validator for whatever you are trying to do, and build out the message there. Otherwise, just translate it manually the way you already figured it out.

Sign up to request clarification or add additional context in comments.

1 Comment

Very nice explanation. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.