我有以下课程
要验证的实体:
<?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()无法正常工作。它将引发以下错误:
第17行的ExceptionListener.php: Symfony \ Component \ Validator \ Exception \ NoSuchMetadataException {#1052
消息:“类或接口“测试”不存在。”
以下是有效负载:
"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"
},
答案 0 :(得分:1)
您使用了错误的验证器。
首先将适当的名称空间导入您的类定义中:
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
注释)。另外,从此类中删除getTargets()
方法。
答案 1 :(得分:0)
您必须使用自定义验证程序断言。当您要验证与您的实体相关的属性时(例如,当您具有“用户到地址”的关系并且希望对用户实体上的@Assert\Valid()
进行一些自定义验证时,应使用$address
批注)。
这就是为什么它寻找类test
进行验证的原因。查看文档here