繁体   English   中英

Symfony Add bootstrap表单控件类

[英]Symfony Add bootstrap form-control class

我正在使用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')
));

如果是“名称”字段,则不会添加该类的密码字段的有效BUT。 如何为密码字段添加“ form-control”类。 任何帮助深表感谢。 谢谢。

有两种方法可以做到这一点。 第一种是使用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')
));

另外, 从Symfony 2.6开始,它具有内置的Bootstrap表单主题支持 ,您不必在其中手动将这些类添加到所有字段中。

来自Symfony开发团队的一些人建议,您应该直接在html中使用boostrap的类(如果要查看建议,请参见此处 )。 这个建议对我来说很完美,因为Symfony用于后端开发,而不是前端。 因此,解决此问题的理想方法是在Type类中创建两个字段,并在渲染表单时添加如下内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM