【问题标题】:Symfony Add bootstrap form-control classSymfony 添加引导表单控制类
【发布时间】:2018-03-02 22:33:38
【问题描述】:

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

$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')
));

在“名称”字段的情况下,它的工作但对于该类未添加的密码字段。 如何为密码字段添加“表单控制”类。 任何帮助深表感谢。 谢谢。

【问题讨论】:

    标签: php twitter-bootstrap forms symfony symfony-2.8


    【解决方案1】:

    有两种方法可以做到这一点。第一种是使用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.6built-in Bootstrap form theme support,您不必手动将这些类添加到所有字段中。

    【讨论】:

    • 太好了,非常感谢:)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-10-12
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 2021-08-09
      • 1970-01-01
      相关资源
      最近更新 更多