du67560 2016-03-22 17:36 采纳率: 0%
浏览 35
已采纳

使用实体验证表单内的FormType

I have two entities: Company and Location. One company has one location (while one location may "have" multiple companies). Now when a user creates a company, I want him/her to able able, to create the location in the same form. So I use the following

    $builder
        ->add('name', TextType::class, ['label' => 'company.name'])
        ->add('size', IntegerType::class, ['label' => 'company.size'])
        ->add( $builder->create('location', FormType::class, [
                'label' => 'company.location',
                'by_reference' => true,
                'data_class' => 'AppBundle\Entity\Location',
            ])
            ->add('street', TextType::class, [
                'label' => 'location.street',
            ])
            ->add('number', TextType::class, [
                'label' => 'location.number',
            ])

This works fine in creating the form. Now it comes to validation. I added @Assert annotations for both entities in their respective files. While company validation works, location does not get automatically validated.

I managed to get validation by adding constraint properties to the new $builder->create('location') elements, but this means duplicated code (once in Entity and at least once in every form that needs location).

How can I solve it so the form gets validated by using the entity's annotation?

  • 写回答

1条回答 默认 最新

  • duanjie6912 2016-03-22 18:55
    关注

    By default validation won't traverse to properties that are objects or collections. Use the valid constraint:

    http://symfony.com/doc/current/reference/constraints/Valid.html

    You cans set the traverse option for collections as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料