php - Symfony form error message parameters usage -
what purpose of message parameters array in symfony form error ?
for example, have following case: on form have subscriber in which, based on information given user api may called , additional errors added symfony form.
as such, when error occurs, add new error on field:
$myform->get('name')->adderror( new formerror('name.api_invalid', null, array('{{ api_name }}' => $somevalue)) );
where 'name.api_invalid' defined in message.en.yml
name.api_invalid: "the api says name {{ api_name }}. please fix before proceeding."
while message translated, "parameters" not replaced.
is not how form error parameters supposed work ?
note: can make work using
$myform->get('name')->adderror( new formerror( $this->translator->trans('name.api_invalid', array('{{ api_name }}' => $somevalue)) ) );
but i'm curious error parameters.
thank you!
the behavior looking @ changed in symfony 2.2:
translating validation errors optional. can still manually if like, or can simplify templates output translated message.
if @ form_errors
block in 2.1 vs. form_errors
block in 2.2 see difference in how errors displayed. override block , old way , import template wherever need it, or can translate error message way you're doing above (which how i've typically done , acceptable).
if you're going route of $translator->trans
use %..%
in parameters, stated in symfony documentation:
the placeholders can take on form full message reconstructed using php strtr function. %...% form recommended, avoid problems when using twig.
the {{ }}
using more relegated using symfony validators , how build violations (example here).
now, if want messages , parameters automatically translated without manually throwing formerror
, suggest creating custom validator whatever trying do, , build out message there. otherwise, translate manually way figured out.
Comments
Post a Comment