Symfony2: where to enter domain_name translation into a form component

I create the form using the Symfony 2 form component. Since verification errors are translated in different translation domains, I want to insert this information as an option (translation_domain) during the creation of the form, but not find the right (successful) place to install ... Any hints?

I use a custom type to bind form information.

My custom type class:

use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\NotBlank; class LoginType extends AbstractType { public function setDefaultOptions(OptionsResolverInterface $resolver) { $collectionConstraint = $collectionConstraint = new Collection(array( 'password' => array(new NotBlank(array('message' => 'custom.error.blank'))), 'username' => array(new NotBlank(array('message' => 'custom.error.blank'))) )); $resolver->setDefaults(array( 'constraints' => $collectionConstraint )); } public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('username', 'text', array( 'max_length' => 250, 'trim' => true )); $builder->add('password', 'password', array( 'max_length' => 250, 'trim' => true )); } public function getName() { return 'login'; } } 

corresponding code fragments when creating a form in the controller:

 $loginForm = $this->createForm(new LoginType(), $loginDefaultData); $loginForm->bind($request); [...] return $this->render( 'MyBundle:SubFolder:login.html.twig', array( 'loginForm' => $loginForm->createView() ) ); 
+6
source share
1 answer

After returning to the problem after a while, I found the reason: dynamic domain transfer could be set to setDefaultOptions, as shown below.

 public function setDefaultOptions(OptionsResolverInterface $resolver) { // ... $resolver->setDefaults(array( 'constraints' => $collectionConstraint, 'translation_domain' => 'customTranslationDomain' )); } 

However, in the twig template used, this domain_domain is used for labels and options, but no error messages are sent with this translational_domain. they are always translated using fixed-set "validators" to the default template template in

/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

It usually makes sense to link these messages in the same domain, but not in my case, because the same restriction (and its error message) must be translated differently depending on the context, and each context was organized in an isolated domain.

My solution was to configure the rendering of the form as described in the Symfony2 documentation , override the form_errors fragment and use the twigring_domain dynamic variable to also output an error message.

+8
source

All Articles