2017-09-21 48 views
2

我正在使用symfony 2.8,我创建了一个注册表单,我想将引导窗体控件类添加到密码和重复密码表单字段中。Symfony添加引导窗体控件类

$builder 
->add('name', TextType::class,array(
    'attr' => array(
     'class' => 'form-control' 
    ) 
)) 
-> add('plainPassword', RepeatedType::class, array(
    'type' => PasswordType::class, 
    'first_options' => array('label' => 'Password'), 
    'second_options' => array('label' => 'Repeat Password'), 
    'attr' => array('class' => 'form-control') 
)); 

将'name'字段置于其工作状态,但对于该类未添加的密码字段。 如何为密码字段添加“表单控件”类。 任何帮助,非常感谢。 谢谢。

回答

2

有两种方法可以做到这一点。第一种方法是使用options,其将通过选择到每个潜在领域:

->add('plainPassword', RepeatedType::class, array(
    'type' => PasswordType::class, 
    'first_options' => array('label' => 'Password'), 
    'second_options' => array('label' => 'Repeat Password'), 
    'options' => array('attr' => array('class' => 'form-control')) 
)); 

您还可以添加类的first_optionssecond_options领域,像这样。如果您有针对每个字段的选项,或者您想覆盖主选项中的某些内容,这将非常有用。

->add('plainPassword', RepeatedType::class, array(
    'type' => PasswordType::class, 
    'first_options' => array(
     'label' => 'Password', 
     'attr' => array('class' => 'form-control') 
    ), 
    'second_options' => array(
     'label' => 'Password', 
     'attr' => array('class' => 'form-control-override') 
    ), 
    'attr' => array('class' => 'form-control') 
)); 

此外,as of Symfony 2.6它具有built-in Bootstrap form theme support到你不应该必须手动添加这些类到所有领域。

+0

大,非常感谢:) – Aamir

1

来自Symfony开发团队的一些人建议您应该直接在html中使用boostrap的类(如果您想查看建议,请使用here)。这个建议对我来说非常敏感,因为Symfony是用于后端开发,而不是前端。因此,解决这一理想的方法是在Type类来创建你的两个领域,并呈现窗体时,添加类似:

{{ form_row(name, { attr: { 'class': 'form-control' }) }} 
{{ form_row(password, { attr: { 'class': 'form-control' }) }} 
{{ form_row(plainPassword, { attr: { 'class': 'form-control' }) }}