0
votes

I'm using Symfony validation within my own app (not Symfony). I'm expecting it to return violations as I haven't populated the Id property:

$user = new User();
$user->setId('');
//...

$validator = \Symfony\Component\Validator\Validation::createValidator();
$errors = $validator->validate($user);
var_dump(count($errors)); exit; // outputs: 0

However, validate returns no violations.

Here is my User class with constraint annotations:

use Symfony\Component\Validator\Constraints as Assert;

class User {

    /**
     * @Assert\NotBlank
     */
    private $id;

    //...

    public function getId(): string {
        return $this->id;
    }

    public function setId(string $id): void {
        $this->id = $id;
    }

    //...

Where am I going wrong? According to the docs, a blank string should trigger a violation for this constraint - https://symfony.com/doc/current/reference/constraints/NotBlank.html

1

1 Answers

0
votes

You don't need Annotations if you use Validation. Try this :

$validator = Validation::createValidator();
$violations = $validator->validate($user->getId, [
    new NotBlank(),
]);