Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: Where to inject translation_domain in Form Component

Tags:

php

symfony

I create a form using the form component of Symfony 2. As the validation errors are translated in different translation domains, I want to inject this information as an option (translation_domain) during creation of the form, but dont find the right (successful) spot where to set... Any hints?

I use a custom type for bundling my form informations.

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';
    }
}

relevant code snippets on form creation in controller:

$loginForm = $this->createForm(new LoginType(), $loginDefaultData);

$loginForm->bind($request);

[...]

return $this->render(
    'MyBundle:SubFolder:login.html.twig',
    array(
        'loginForm' => $loginForm->createView()
    )
);
like image 769
Skug Avatar asked Nov 08 '12 22:11

Skug


1 Answers

After returning to the problem a while later, I found the reason: the dynamic translation_domain can be set within setDefaultOptions as followed.

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    // ...

    $resolver->setDefaults(array(
        'constraints'        => $collectionConstraint,
        'translation_domain' => 'customTranslationDomain'
    ));
}

However, in the used twig template, this translation_domain is used for labels and options, but the error messages are not served with this translation_domain. they are always translated with the same fixed set domain 'validators' in the default twig template at

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

It normally makes sense to bundle these messages in one domain, but not in my case, as the same constraint (and its error message) has to be translated in different ways depending on context and each context was organized in a isolated domain.

My solution was to customize the form rendering as described in the Symfony2 documentation, redefine the form_errors fragment and use the dynamic translation_domain twig variable also for the error message output.

like image 168
Skug Avatar answered Oct 05 '22 22:10

Skug