【问题标题】:uuid_binary form validation error: The choice does not exist or is not uniqueuuid_binary 表单验证错误:选择不存在或不唯一
【发布时间】:2016-07-05 14:04:48
【问题描述】:

我正在使用ramsey/uuid-doctrine 包将二进制 UUID 作为实体的主键:

class Site
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     * @ORM\Column(type="uuid_binary")
     */
    private $id;

    ...
} 

我需要为另一个实体构建一个表单,该实体在多对一关系中具有该实体。

所以我在表单构建器中添加了一个 EntityType,如下所示:

$builder->add('site')

一切正常,HTML 选择将 uuid 作为键,但是当我提交表单时,我收到此验证错误:

Symfony\Component\Validator\ConstraintViolation Object(Symfony\Component\Form\Form).children[site] = 4a41b3c4-4299-11e6-b562-fc3fdb5f816d

原因: Symfony\Component\Form\Exception\TransformationFailedException 无法反转属性路径“站点”的值:选项“4a41b3c4-4299-11e6-b562-fc3fdb5f816d”不存在或不唯一

原因: Symfony\Component\Form\Exception\TransformationFailedException 选项“4a41b3c4-4299-11e6-b562-fc3fdb5f816d”不存在或不唯一


如果我这样做,在控制器中:

$this->getDoctrine()->getManager()
  ->getRepository('AppBundle:Site')->find('4a41b3c4-4299-11e6-b562-fc3fdb5f816d');

它确实找到了实体,所以它存在。

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    我能找到的唯一 hack:我从请求中删除了字段值并稍后手动设置它

    $site_uuid = $_POST['smart_link']['site'] ?? null;
    
    $request_link = $request->request->get('link');
    $request_link['site'] = '';
    $request->request->set('link', $request_link);
    
    $form->handleRequest($request);
    
    ....
    
    $link->setSite($em->getRepository('AppBundle:Site')->find($site_uuid));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-02
      • 2021-04-25
      • 2017-10-04
      • 2020-04-14
      • 2011-11-02
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多