doujie1908 2017-09-21 14:10
浏览 104
已采纳

Symfony添加bootstrap表单控件类

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.

$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. How can I add 'form-control' class for password fields. Any help is much appreciated. Thanks.

  • 写回答

2条回答 默认 最新

  • doujiao7520 2017-09-21 14:22
    关注

    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:

    ->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. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目