简体   繁体   English

Symfony / Doctrine:在提交表单之前,symfony会将关系的ID解析为实体

[英]Symfony/Doctrine: Validating ID of relation BEFORE symfony resolves it to an entity on form submission

class Thing {

    /*** loads of properties ***/

    /**
     * This is a many to many relation from Thing to Tags

     * @var \Doctrine\Common\Collections\Collection
     *
     * @Assert\All({
     *      @Assert\Uuid()
     * })
     */
    private $tags;

    /*** moar stuff ***/

}    

    /*** On the controller ***/

    protected function validateAndPersist(Request $request, AbstractEntity $entity, $successHttpCode)
    {
        $form = $this->createForm($this->getFormDefinition(), $entity);

        $form->submit($request);
        if ($form->isValid() === true) {
            $this->getDoctrineManager()->persist($entity);
            $this->getDoctrineManager()->flush();

            return $this->createView($entity, $successHttpCode);
        }

        return $form;
    }

So I have this entity above, which has a matching Symfony form and it's all tied up nicely by a controller. 所以我上面有这个实体,它具有匹配的Symfony形式,并且都被控制器很好地捆绑了起来。 The payload coming in is meant to send an array of UUIDs on to the tags property. 传入的有效负载旨在将UUID数组发送到tags属性。 This works just fine, relations are saved correctly. 这很好,关系已正确保存。

So there's some validation in place to make sure we're receiving an array of UUIDs. 因此,已经进行了一些验证,以确保我们接收到一组UUID。

Problem is, however, precisely when I'm not receiving an array of UUIDs. 但是,问题恰恰是当我没有收到UUID数组时。 $form->submit() is trying to resolve the incoming IDs into actual tags via doctrine. $form->submit()试图通过学说将传入的ID解析为实际的标签。 The postgres database is complaining that it's not a valid UUID (the relevant field is of type guid), and I get a Doctrine error. postgres数据库抱怨它不是有效的UUID(相关字段的类型为guid),并且出现了Doctrine错误。 It feels as if this is happening BEFORE form validation, or perhaps validation is not performed at all on this field because of the many to many relation. 感觉这似乎是在表单验证之前发生的,或者由于多对多的关系,可能根本没有在此字段上执行验证。

Either way this results on a Doctrine\\DBAL\\DBALException on which the message includes details of the SQL query and whatnot and a big juicy 500 error: 无论哪种方式Doctrine\\DBAL\\DBALExceptionDoctrine\\DBAL\\DBALException上导致该消息包含SQL查询和诸如此类的详细信息以及一个多汁的500错误:

    {
      "code": 500,
      "message": "An exception occurred while executing 'SELECT t0_.tag_id AS tag_id_0, t0_.name AS name_1 FROM tag t0_ WHERE t0_.tag_id IN (?)' with params [\"comedy\"]:\n\nSQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for uuid: \"comedy\""
    }

What I would obviously want is for validation defined on the entity to actually happen, as that would trigger a proper HTTP response without any boilerplate code to handle this specific case. 我显然想让实体上定义的验证真正发生,因为这将触发适当的HTTP响应,而无需任何样板代码来处理这种特定情况。

Any ideas? 有任何想法吗?

Are you using another an embedded form for Tags in the main form? 您是否在主表单中使用另一个嵌入表单来标记? In this case, you need to set cascade_validation property to true on it. 在这种情况下,您需要对其设置cascade_validation属性为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM