predaddy / predaddy-symfony-validator
Symfony validation component for predaddy
dev-master
2014-11-09 09:54 UTC
Requires
- php: >=5.4
- lf4php/lf4php: ~4.2
- predaddy/predaddy: 3.0.*@dev
- symfony/validator: ~2.5
Requires (Dev)
- lf4php/lf4php-log4php: ~2.1
- phpunit/phpunit: ~4.1
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-10-08 03:41:01 UTC
README
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');