我使用 Symfony 2 的表单组件创建了一个表单。由于验证错误在不同的翻译域中翻译,我想在创建表单期间将此信息作为选项 (translation_domain) 注入,但找不到正确的 (成功) 点在哪里设置...任何提示?
我使用自定义类型来捆绑我的表单信息。
我的自定义类型类:
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';
}
}
在控制器中创建表单的相关代码片段:
$loginForm = $this->createForm(new LoginType(), $loginDefaultData);
$loginForm->bind($request);
[...]
return $this->render(
'MyBundle:SubFolder:login.html.twig',
array(
'loginForm' => $loginForm->createView()
)
);