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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵