dev-master
9999999-devValidator implementation lib
MIT
The Requires
- php >=5.3.3
- symfony/validator ~2.5
The Development Requires
Validator implementation lib
Validation tool that can be connected on any object you want to validate., (*2)
Validators are classes that will determines if the value was valid.
In the following example, the YourNonEmptyValidator
would make sure the name on the SomeObject
cannot be empty., (*3)
// YourNonEmptyValidator.php class YourNonEmptyValidator implements Validator { /** * @var SomeObject */ private $object; /** * @param SomeObject $object */ public function __construct(SomeObject $object) { $this->object = $object; } /** * @param NotificationHandler $handler * * @return ValidationResult */ public function validate(NotificationHandler $handler) { $name = $this->object->getName(); if (empty($name)) { $handler->notifyError(new StringMessage('Name cannot be empty.')); } return $handler->createResult(); } }
The validators will return a ValidationResult
to determine whether any constraints have failed., (*4)
The NotificationHandler
are responsible to notify the user about an error., (*5)
The only supported strategy currently implemented are:, (*6)
ValidationErrorException
on the first error (recommended for development).ValidationResult
.Given you created a class that needs to be validated:, (*7)
// SomeObject.php class SomeObject { /** * @var string */ private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } }
You will need to implement a validation method in which you will put all the validators to push in the ValidationHandler
., (*8)
// SomeObject.php ... /** * @param NotificationHandler $handler * * @return ValidationResult */ public function validate(NotificationHandler $handler) { $validator = new YourNonEmptyValidator($this); // This is your custom validator implementing Validator interface return $validator->validate($handler); } ...
Creating the validator for your object (or using built-in validators, you can get the result from the validation., (*9)
By using the structure, validating your code will look something like this., (*10)
// Using the Exception handler on a valid object $validObject = new SomeObject(); $validObject->setName('non-empty'); $result = $validObject->validate(new ExceptionNotificationHandler()); $result->hasErrors(); // Returns false $result->getErrors()); // Returns empty array(), since there was no errors // Using the Exception handler on a invalid object $invalidObject = new SomeObject(); $invalidObject->validate(new ExceptionNotificationHandler()); // Would throw an exception on the first error (because of the handler). // Using the deferred handler on a invalid object $result = $invalidObject->validate(new DeferredNotificationHandler()); $result->hasErrors(); // Returns true $result->getErrors()); // Returns empty array('Name cannot be empty.')
Validator implementation lib
MIT