@zipo2

Symfony 3.4.6 custom validator в чём может быть ошибка?

Пробывал сделать кастомный валидатор, скопировал всё с документации https://symfony.com/doc/3.4/validation/custom_cons...

Выдаёт такую ошибку [Semantical Error] The annotation "@AppBundle\Validator\Constraints\ContainsAlphanumeric" in property AppBundle\Entity\Category::$name does not exist, or could not be auto-loaded.

Может ктонибудь помочь?

<?php
/* File AppBundle\Validator\Constraints\ContainsAlphunumeric.php */
namespace AppBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class ContainsAlphanumeric extends Constraint
{
    public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';

    public function validatedBy()
    {
        return get_class($this).'Validator';
    }
}
?>


<?php
/* File AppBundle\Validator\Constraints\ContainsAlphanumericValidator.php */
namespace AppBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ContainsAlphanumericValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ string }}', $value)
                ->addViolation();
        }
    }
}
?>


<?php
/* File AppBundle\Entity\Category.php*/
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Validator\Constraints\ContainsAlphanumeric;
/*....*/
/**
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     * @Groups({"Self"})
     * @ContainsAlphanumeric
     */
    protected $name;
?>
  • Вопрос задан
  • 452 просмотра
Решения вопроса 1
BoShurik
@BoShurik Куратор тега Symfony
Symfony developer
/* File AppBundle\Validator\Constraints\ContainsAlphunumeric.php */
class ContainsAlphanumeric extends Constraint
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
19 апр. 2024, в 12:53
1000 руб./за проект
19 апр. 2024, в 12:41
8000 руб./за проект
19 апр. 2024, в 12:05
1500 руб./в час