Foregin Key的Symfony 2验证约束

时间:2023-02-10 22:49:36

I am building an Restful API where say 'userId' / 'addressId' are POST/PUT (between other fileds) and these fields are foreign keys in the DB.

我正在构建一个Restful API,其中'userId'/'addressId'是POST / PUT(在其他文件之间),这些字段是DB中的外键。

My question is about Symfony Validation Component as I want to validate if those id's are valid.

我的问题是关于Symfony验证组件,因为我想验证这些id是否有效。

This, I think, need to be a 'Doctrine' validator as it will need to query the related tables.

我认为,这需要是一个'Doctrine'验证器,因为它需要查询相关的表。

I checked the existing validators and it seems that none of them cover this case, in fact, it is only one Doctrine validator (UniqueEntityValidator)

我检查了现有的验证器,似乎没有一个覆盖这种情况,事实上,它只是一个Doctrine验证器(UniqueEntityValidator)

I am writing a custom validatior at this stage.. but did anyone else find the same issue ? Is it not any standard Symfony/Doctrine validator available for this case?

我在这个阶段写了一个自定义验证器..但其他人是否也发现了相同的问题?对于这种情况,它不是任何标准的Symfony / Doctrine验证器吗?

1 个解决方案

#1


0  

You do not have to write a custom validator. Symfony has already an object validation functionality. You need to set type option to full name of an entity you want to validate.

您不必编写自定义验证器。 Symfony已经具有对象验证功能。您需要将type选项设置为要验证的实体的全名。

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\User:
    properties:
        address:
            - Type:
                type: AppBundle\Entity\Address

#1


0  

You do not have to write a custom validator. Symfony has already an object validation functionality. You need to set type option to full name of an entity you want to validate.

您不必编写自定义验证器。 Symfony已经具有对象验证功能。您需要将type选项设置为要验证的实体的全名。

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\User:
    properties:
        address:
            - Type:
                type: AppBundle\Entity\Address