duandeng1824 2015-06-16 17:34
浏览 37
已采纳

在Symfony2表单文本字段强制大写

I've inherited a Symfony2 project that has a form built as such:

        $builder
        ->add('Status', 'choice', array(
          'choices' => array('' => '', 'PASS' => 'PASS', 'FAIL' => 'FAIL', 'INCOMPLETE' => 'INCOMPLETE', 'DROPPED' => 'DROPPED',),
          'required' => FALSE,
        ))
        ->add('First_Name', 'text', array('label'=>'First Name',
          'required' => FALSE))
        ->add('Last_Name', 'text', array('label'=>'Last Name',
          'required' => FALSE))
        ->add('PID', 'text', array('label'=>'License Number',
          'required' => FALSE))
       ;

I need to have any letters entered in the PID field to be forced to uppercase. However I cannot find a option/attribute/... to do this.

Is there any way to do this?

  • 写回答

2条回答 默认 最新

  • dongwen1909 2015-06-16 18:05
    关注

    I’m not sure if you ask for server-side validation or assistance for client-side uppercase input. In case of the latter: you could add a CSS class or a data-* attribute (something like ->add('PID', 'text', ['label'=>'License Number', 'required' => FALSE, 'attr' => ['data-behavior' => 'uppercase']])) to the PID element. Then, you could add a JavaScript event handler in the form (preferably using jQuery or some other framework) to automatically convert lowercase to uppercase letters on blur/change. (I would prefer the data-* attribute, as this has nothing to do with CSS.)

    To enforce that on the server-side, you could use a @Assert\Regex annotation.

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

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题