忽略组的 symfony 5.4 验证约束
symfony 5.4 validation constraints with groups are ignored
此约束注释有效:
use App\Api\Dto\DtoInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Symfony\Component\Serializer\Annotation\Groups as SerializerGroups;
use Symfony\Component\Validator\Constraints as Assert;
class Report implements DtoInterface
{
/**
* @OA\Property(description="visited house id SAP format 4 character string", type="string")
*
* @SerializerGroups({"create", "update", "view", "collection"})
*
* @Assert\NotBlank
* @Assert\Length(4)
*/
public string $house = '';
而这不是
use App\Api\Dto\DtoInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Symfony\Component\Serializer\Annotation\Groups as SerializerGroups;
use Symfony\Component\Validator\Constraints as Assert;
class Report implements DtoInterface
{
/**
* @OA\Property(description="visited house id SAP format 4 character string", type="string")
*
* @SerializerGroups({"create", "update", "view", "collection"})
*
* @Assert\NotBlank(groups={"create", "update"})
* @Assert\Length(min=4, groups={"create", "update"})
*/
public string $house = '';
幸运的是,在这种情况下,忽略组对我来说仍然有效,但在其他情况下可能不会。
Symfony documentation 说它应该这样工作。
我的第二个例子有什么问题?为什么这些验证器被忽略了?
请注意,在第二次检测中,在验证器中检测到 @Assert\NotBlank(groups={"create", "update"})
个绝育组,与第一个示例相反,这意味着它们仅呈现给给定的组,而所有其他请求都带有未指定的负载将被忽略。
换句话说,第二个例子中的限制只适用于create
和update
,而对于view
和collection
组,它们将被忽略, 因为它们没有在注释中指定。
此约束注释有效:
use App\Api\Dto\DtoInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Symfony\Component\Serializer\Annotation\Groups as SerializerGroups;
use Symfony\Component\Validator\Constraints as Assert;
class Report implements DtoInterface
{
/**
* @OA\Property(description="visited house id SAP format 4 character string", type="string")
*
* @SerializerGroups({"create", "update", "view", "collection"})
*
* @Assert\NotBlank
* @Assert\Length(4)
*/
public string $house = '';
而这不是
use App\Api\Dto\DtoInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Symfony\Component\Serializer\Annotation\Groups as SerializerGroups;
use Symfony\Component\Validator\Constraints as Assert;
class Report implements DtoInterface
{
/**
* @OA\Property(description="visited house id SAP format 4 character string", type="string")
*
* @SerializerGroups({"create", "update", "view", "collection"})
*
* @Assert\NotBlank(groups={"create", "update"})
* @Assert\Length(min=4, groups={"create", "update"})
*/
public string $house = '';
幸运的是,在这种情况下,忽略组对我来说仍然有效,但在其他情况下可能不会。
Symfony documentation 说它应该这样工作。
我的第二个例子有什么问题?为什么这些验证器被忽略了?
请注意,在第二次检测中,在验证器中检测到 @Assert\NotBlank(groups={"create", "update"})
个绝育组,与第一个示例相反,这意味着它们仅呈现给给定的组,而所有其他请求都带有未指定的负载将被忽略。
换句话说,第二个例子中的限制只适用于create
和update
,而对于view
和collection
组,它们将被忽略, 因为它们没有在注释中指定。