简体   繁体   English

Symfony Add bootstrap表单控件类

[英]Symfony Add bootstrap form-control class

I'm using symfony 2.8, I have created one registration form, I want to add bootstrap form-control class to both password and repeat password form fields. 我正在使用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')
));

Incase of 'name' field its working BUT for password fields the class is not adding. 如果是“名称”字段,则不会添加该类的密码字段的有效BUT。 How can I add 'form-control' class for password fields. 如何为密码字段添加“ form-control”类。 Any help is much appreciated. 任何帮助深表感谢。 Thanks. 谢谢。

There are two ways of doing this. 有两种方法可以做到这一点。 The first is to use options , which will pass the options down to each of the underlying fields: 第一种是使用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'))
));

You can also add the class in the first_options and second_options field, like so. 您也可以像这样在first_optionssecond_options字段中添加该类。 This would be useful if you had options that were specific to each field or you wanted to override something from the main 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')
));

Also, as of Symfony 2.6 it has has built-in Bootstrap form theme support to where you shouldn't have to be adding these classes to all of your fields manually. 另外, 从Symfony 2.6开始,它具有内置的Bootstrap表单主题支持 ,您不必在其中手动将这些类添加到所有字段中。

Some guys from the Symfony development team, suggest that you should use the boostrap's classes directly in html ( here , if you want to see the suggestion). 来自Symfony开发团队的一些人建议,您应该直接在html中使用boostrap的类(如果要查看建议,请参见此处 )。 And the suggestion makes perfect sens to me, as Symfony is for backend development, and not frontend. 这个建议对我来说很完美,因为Symfony用于后端开发,而不是前端。 So the ideal way of solving this is to create your two fields in the Type class, and when rendering the form, add something like: 因此,解决此问题的理想方法是在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