symnedi / validator
Symfony\Validator integration to Nette.
Installs: 15 754
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=5.6
- kdyby/annotations: ~2.2
- nette/caching: ~2.3
- nette/di: ~2.3
- symfony/validator: ~2.7
Requires (Dev)
- nette/bootstrap: ~2.3
This package is not auto-updated.
Last update: 2022-02-01 12:47:42 UTC
README
This package is deprecated. Use Kdyby/Validator instead.
Install
Via Composer:
$ composer require symnedi/validator
Register the extension in config.neon
:
extensions: - Symnedi\Validator\DI\ValidatorExtension - Kdyby\Annotations\DI\AnnotationsExtension
Usage
Let's register user, but only while having valid email.
First, use validation annotations on entity (object) you want to validate.
use Symfony\Component\Validator\Constraints as Assert; class User { /** * @Assert\NotBlank * @Assert\Email */ private $email; /** * @var string $email */ public function __construct($email) { $this->email = $email; } }
Then validate in our service:
use Symfony\Component\Validator\Validator\ValidatorInterface; class RegistrationManager { /** * @var ValidatorInterface */ private $validator; public function __construct(ValidatorInterface $validator) { $this->validator = $validator; } /** * Instance is passed: $user = new User('invalid.email'); */ public function registerUser(User $user) { $violations = $this->validator->validate($user); // process violations $violation = $violations[0]; $violation->getMessage(); // 'Email is not valid.' } }
That's it!
For detailed usage, check Symfony documentation.
Testing
$ phpunit
Contributing
Please see CONTRIBUTING for details.