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++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式
  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题