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条)

报告相同问题?

悬赏问题

  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败