predaddy/predaddy-symfony-validator

Symfony validation component for predaddy

dev-master 2014-11-09 09:54 UTC

This package is not auto-updated.

Last update: 2024-11-19 04:13:33 UTC


README

Latest Stable Version Scrutinizer Code Quality SensioLabsInsight Gitter chat

The ValidationInterceptor provided by this library helps validating messages posted to predaddy MessageBus. It is based on Symfony Validator component.

Usage

$bus = SimpleMessageBus::builder()
    ->withInterceptors([new ValidatorInterceptor()])
    ->build();
class CreateUser
{
    /**
     * @Assert\Length(min = 3)
     * @Assert\NotBlank
     */
    private $name;

    /**
     * @Assert\Email
     * @Assert\NotBlank
     */
    private $email;

    public function __construct($name, $email)
    {
        $this->name = $name;
        $this->email = $email;
    }

    /**
     * @Assert\True(message = "The user should have a Google Mail account")
     */
    public function isGmailUser()
    {
        return false !== strpos($this->email, '@gmail.com');
    }

    public function __toString()
    {
        return Objects::toStringHelper($this)
            ->add('name', $this->name)
            ->add('email', $this->email)
            ->toString();
    }
}
try {
    $bus->post(new CreateUser('John Doe', 'john@example.com'));
} catch (ValidationException $e) {
    $e->getViolationList();
}

If you use annotation constraints, do not forget to register its namespace:

AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\Validator\Constraint', 'path/to/symfony/library/validator');