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 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型