symfony-bundle
symfony-bundle copied to clipboard
New extractor for constraints
Hello, since the bundle doesn't automatically do this I created an extractor which should be able to get all the transactions from all the constraints:
https://github.com/php-translation/extractor/pull/160
In the following example the extracted string would be Inserisci la nuova password.
.
class ChangePasswordFormType extends AbstractType
{
/**
* @param mixed[] $options
* @param FormBuilderInterface<FormBuilderInterface|string> $builder
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'first_options' => [
'constraints' => [
new NotBlank([
'message' => 'Inserisci la nuova password.',
]),
new Length([
'min' => 6,
'minMessage' => 'La nuova password deve essere di almeno {{ limit }} caratteri.',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
'label' => 'Nuova password',
],
'second_options' => [
'label' => 'Ripeti password',
],
'invalid_message' => 'Le due password devono corrispondere.',
// Instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([]);
}
}
@lukepass Hi, thank you for your contribution! It sounds great, but I'm wondering if the best practice for this case is to inject the Translator in your FormTypes / Controller, instead of assume that constraints messages will be filtered in templates with |trans
?
WDYT @Nyholm?
Hello @welcoMattic, you could inject the translator into the Form / Controller but when using new Assert\XYZ(['message' => 'ABC'])
the frontend is already trying to translate the "ABC" string. Using the translator service would "double translate" that string.
If you open the profiler toolbar you would see that the "ABC" string is in the validators domain, without the need to call the trans
method of the translator service.
I updated the pull request with more features and a small test suite.
@lukepass what @welcoMattic might mean is whether translating should be a responsibility of the form or of the view.
IMHO the best practice would be that the view is responsible for translating, as otherwise any class that processes text to appear on a view could eventually need a dependency on the translator.
Does not mean this wouldn't be a useful feature for the bundle though!
@rvanlaak I'm not sure I understand your point. Every time you use a NotBlank / NotNull etc...
with a message (the message can be either a string or a key) the view will already try to translate that message.
It's the same as the constraint annotations (a feature already supported in this bundle). What this bundle isn't supporting right now is a constraint added with the constraints
property of a form type. This can be useful for a not mapped property or a constraint added only to a specific form.
Thanks.