Z
Z
zipo22018-03-19 13:10:54
symfony
zipo2, 2018-03-19 13:10:54

Symfony 3.4.6 custom validator what could be wrong?

I tried to make a custom validator, copied everything from the documentation https://symfony.com/doc/3.4/validation/custom_cons...
Gives the following error [Semantical Error] The annotation "@AppBundle\Validator\Constraints\ContainsAlphanumeric" in property AppBundle\ Entity\Category::$name does not exist, or could not be auto-loaded.
Can anyone help?

<?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;
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2018-03-19
@zipo2

/* File AppBundle\Validator\Constraints\ContainsAlph u numeric.php */
class ContainsAlph a numeric extends Constraint

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question