为什么我的自定义 Symfony 验证器没有执行?
Why isn't my custom Symfony validator executed?
我有以下类
要验证的实体:
<?php
//CompanyName\DataBundle\Entity\Intern\Address.php
namespace CompanyName\DataBundle\Entity\Intern;
use CompanyName\DataBundle\Entity\Country;
use Swagger\Annotations\Property;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use CompanyName\ConstraintValidatorBundle\ConstraintValidator;
use CompanyName\ConstraintValidatorBundle\Entity\CountryZipCodeAwareInterface;
/**
* Class Address
* @package CompanyName\DataBundle\Entity\Intern
* @ConstraintValidator\ZipCode
*/
class Address implements CountryZipCodeAwareInterface
{
/**
* originId for 'API' send to the Addressbridge and stored in the Addresservice
*
*
*
*/
const ORIGIN_ID_FOR_ADDRESS_FROM_API = 2;
/**
* @var integer
*/
private $id;
/**
* @var Name
* @Property(type="string")
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @Assert\Valid()
*/
private $name;
/**
* @var string
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @Assert\Type("string")
* @Assert\Length(max = 100, maxMessage = "Street name cannot be longer than {{ limit }} characters")
*/
private $street;
/**
* @var string
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @Assert\Type("string")
* @Assert\Regex("/^\d+$/")
* @Assert\Length(max = 5, maxMessage = "House number cannot be longer than {{ limit }} characters")
*/
private $housenumber;
/**
* @var string
* @Groups({"createOrder", "shoppingCart"})
* @Assert\Type("string")
* @Assert\Length(max = 25, maxMessage = "Addition cannot be longer than {{ limit }} characters")
/**
* @var Country
* @Property(type="string")
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @Assert\Valid()
*/
private $country;
}
在我的自定义验证器中使用的约束:
<?php
//CompanyName\DataBundle\Validator\Name.php
namespace CompanyName\DataBundle\Validator;
use Symfony\Component\Validator\Constraint;
/**
* Class Name
* @package CompanyName\DataBundle\Validator
* @Annotation
*/
class Name extends Constraint
{
public $message = "No Valid Name provided";
/**
* {@inheritdoc}
*/
public function getTargets()
{
die("gggggg");
return self::CLASS_CONSTRAINT;
}
}
实际验证者:
<?php
//CompanyName\DataBundle\Validator\NameValidator.php
namespace CompanyName\DataBundle\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class NameValidator extends ConstraintValidator
{
/**
* Checks if the passed value is valid.
*
* @param Name name The value that should be validated
* @param Constraint $constraint The constraint for the validation
*/
public function validate($name, Constraint $constraint)
{
var_dump($name);
die();
}
}
Assert\Valid() 无效。它抛出以下错误:
ExceptionListener.php on line 17:
Symfony\Component\Validator\Exception\NoSuchMetadataException {#1052
message: "The class or interface "test" does not exist."
以下是有效载荷:
"invoiceAddress": {
"name": "test",
"street": "Dorpsstraat",
"housenumber": "123",
"zipcode": "1234AB",
"city": "Dorp",
"country": "nl"
},
"deliveryAddress": {
"name": "Naam",
"street": "Dorpsstraat",
"housenumber": "123",
"housenumberAddition": "A",
"zipcode": "1234AB",
"city": "Dorp",
"country": "nl"
},
您必须使用自定义验证器断言。 @Assert\Valid()
注释应该在你想要验证与你的实体相关的 属性 时使用(例如,当你有 User to Address 关系并且你想在 User 处对 $address
进行一些自定义验证实体)。
这就是它寻找 class test
进行验证的原因。查看文档 here
您使用了错误的验证器。
首先将适当的命名空间导入到您的 class 定义中:
use CompanyName\DataBundle\Validator as CompanyAsserts;
然后您的 属性 注释应该如下所示:
/**
* @var Name
* @Property(type="string")
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @CompanyAsserts\Name
*/
private $name;
这样你实际上是在使用你的自定义验证器。删除 @Assert\Valid()
、that's not the validatior you are looking for.
您还需要更新约束注释:
/**
* Class Name
* @package CompanyName\DataBundle\Validator
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*/
class Name extends Constraint {
// your class
}
(注意 @Target
注释)。此外,从 class.
中删除 getTargets()
方法
我有以下类
要验证的实体:
<?php
//CompanyName\DataBundle\Entity\Intern\Address.php
namespace CompanyName\DataBundle\Entity\Intern;
use CompanyName\DataBundle\Entity\Country;
use Swagger\Annotations\Property;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use CompanyName\ConstraintValidatorBundle\ConstraintValidator;
use CompanyName\ConstraintValidatorBundle\Entity\CountryZipCodeAwareInterface;
/**
* Class Address
* @package CompanyName\DataBundle\Entity\Intern
* @ConstraintValidator\ZipCode
*/
class Address implements CountryZipCodeAwareInterface
{
/**
* originId for 'API' send to the Addressbridge and stored in the Addresservice
*
*
*
*/
const ORIGIN_ID_FOR_ADDRESS_FROM_API = 2;
/**
* @var integer
*/
private $id;
/**
* @var Name
* @Property(type="string")
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @Assert\Valid()
*/
private $name;
/**
* @var string
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @Assert\Type("string")
* @Assert\Length(max = 100, maxMessage = "Street name cannot be longer than {{ limit }} characters")
*/
private $street;
/**
* @var string
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @Assert\Type("string")
* @Assert\Regex("/^\d+$/")
* @Assert\Length(max = 5, maxMessage = "House number cannot be longer than {{ limit }} characters")
*/
private $housenumber;
/**
* @var string
* @Groups({"createOrder", "shoppingCart"})
* @Assert\Type("string")
* @Assert\Length(max = 25, maxMessage = "Addition cannot be longer than {{ limit }} characters")
/**
* @var Country
* @Property(type="string")
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @Assert\Valid()
*/
private $country;
}
在我的自定义验证器中使用的约束:
<?php
//CompanyName\DataBundle\Validator\Name.php
namespace CompanyName\DataBundle\Validator;
use Symfony\Component\Validator\Constraint;
/**
* Class Name
* @package CompanyName\DataBundle\Validator
* @Annotation
*/
class Name extends Constraint
{
public $message = "No Valid Name provided";
/**
* {@inheritdoc}
*/
public function getTargets()
{
die("gggggg");
return self::CLASS_CONSTRAINT;
}
}
实际验证者:
<?php
//CompanyName\DataBundle\Validator\NameValidator.php
namespace CompanyName\DataBundle\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class NameValidator extends ConstraintValidator
{
/**
* Checks if the passed value is valid.
*
* @param Name name The value that should be validated
* @param Constraint $constraint The constraint for the validation
*/
public function validate($name, Constraint $constraint)
{
var_dump($name);
die();
}
}
Assert\Valid() 无效。它抛出以下错误:
ExceptionListener.php on line 17: Symfony\Component\Validator\Exception\NoSuchMetadataException {#1052
message: "The class or interface "test" does not exist."
以下是有效载荷:
"invoiceAddress": {
"name": "test",
"street": "Dorpsstraat",
"housenumber": "123",
"zipcode": "1234AB",
"city": "Dorp",
"country": "nl"
},
"deliveryAddress": {
"name": "Naam",
"street": "Dorpsstraat",
"housenumber": "123",
"housenumberAddition": "A",
"zipcode": "1234AB",
"city": "Dorp",
"country": "nl"
},
您必须使用自定义验证器断言。 @Assert\Valid()
注释应该在你想要验证与你的实体相关的 属性 时使用(例如,当你有 User to Address 关系并且你想在 User 处对 $address
进行一些自定义验证实体)。
这就是它寻找 class test
进行验证的原因。查看文档 here
您使用了错误的验证器。
首先将适当的命名空间导入到您的 class 定义中:
use CompanyName\DataBundle\Validator as CompanyAsserts;
然后您的 属性 注释应该如下所示:
/**
* @var Name
* @Property(type="string")
* @Groups({"createOrder", "shoppingCart"})
* @Assert\NotBlank()
* @CompanyAsserts\Name
*/
private $name;
这样你实际上是在使用你的自定义验证器。删除 @Assert\Valid()
、that's not the validatior you are looking for.
您还需要更新约束注释:
/**
* Class Name
* @package CompanyName\DataBundle\Validator
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*/
class Name extends Constraint {
// your class
}
(注意 @Target
注释)。此外,从 class.
getTargets()
方法