Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New extractor for constraints #450

Open
lukepass opened this issue Jul 27, 2021 · 5 comments
Open

New extractor for constraints #450

lukepass opened this issue Jul 27, 2021 · 5 comments

Comments

@lukepass
Copy link

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:

php-translation/extractor#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([]);
    }
}
@welcoMattic
Copy link
Member

@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?

@lukepass
Copy link
Author

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.

@lukepass
Copy link
Author

I updated the pull request with more features and a small test suite.

@rvanlaak
Copy link
Member

@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!

@lukepass
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants