У меня есть следующие классы
Проверяемая сущность:
<?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 в строке 17: 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"
},