symnedi/validator

This package is abandoned and no longer maintained. The author suggests using the kdyby/validator package instead.

Symfony\Validator integration to Nette.

v0.1.1 2015-11-03 18:03 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:47:42 UTC


README

This package is deprecated. Use Kdyby/Validator instead.

Build Status Quality Score Code Coverage Downloads Latest stable

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.