ray / validate-module
Validation module for Ray.Di
Installs: 48 249
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 3
Open Issues: 0
Requires
- php: >=5.5.0
- ray/di: ^2.5
Requires (Dev)
- phpunit/phpunit: ^4.8
README
Installation
Composer install
$ composer require ray/validate-module
Module install
use Ray\Di\AbstractModule; use Ray\AuraSqlModule\AuraSqlModule; use Ray\AuraSqlModule\Annotation\AuraSqlConfig; class AppModule extends AbstractModule { protected function configure() { $this->install(new ValidateModule); } }
Usage
Annotate target method with @Valid
annotation.
use Ray\Validation\Annotation\Valid; class User { /** * @Valid */ public function createUser($name) { // ... }
Provide onValidate
annotated method in same class for validation. Method can be named freely.
use Ray\Validation\Annotation\OnValidate; // ... /** * @OnValidate */ public function onValidate($name) { $validation = new Validation; if (! is_string($name)) { $validation->addError('name', 'name should be string'); } return $validation; }
Validate all parameters in validation method. addError($name, $message)
with invalid parameter name and message in the case of validation failed.
Ray\Validation\Exception\InvalidArgumentException
thrown on validation failed, But if @OnFailure
annoted method exists, The result of @OnFailure
method returns instead of original.
use Ray\Validation\Annotation\OnFailure; // ... /** * @OnFailure */ public function onFailure(FailureInterface $failure) { // original parameters list($this->defaultName) = $failure->getInvocation()->getArguments(); // errors foreach ($failure->getMessages() as $name => $messages) { foreach ($messages as $message) { echo "Input '{$name}': {$message}" . PHP_EOL; } } }
If you need multiple validation in one class, You can name validation at annotation property as follows.
use Ray\Validation\Annotation\Valid; use Ray\Validation\Annotation\OnValidate; use Ray\Validation\Annotation\OnInvalid; // ... /** * @Valid("foo") */ public function fooAction($name, $address, $zip) { /** * @OnValidate("foo") */ public function onValidateFoo($name, $address, $zip) { // ... /** * @OnInvalid("foo") */ public function onInvalidFoo(FailureInterface $failure) { // ...
Demo
$ php docs/demo/run.php
// It works!