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() ) );
source share