zendframework/zf2 — pull request #5067 by bakura10 (2013-09-03 14:43:59+00:00)
Title: [WIP] [ZF3] Validators refactoring

Hi everyone,

Again another PR/issue for ZF3. I promise, this is the last one I will make (ok, maybe Forms too :D...).

This one is another lower-level component, but I need this one to correctly finish the InputFilter one.

As for filter, the main changes will be: some performance optimizations we can make with PHP 5.4, cleaning some code, homogenize options by using underscore_separated... The only real change I've made for now is to make the ValidatorChain exactly the same as FilterChain (I was always bothered that those classes didn't have the same method names or underlying implementation).

HOWEVER, there is a change @ocramius and I discussed on IRC and I'd love to have your opinion on that. The InputFilter refactor made the component completely stateless. It opened interesting optimizations, and the idea came to make validators also stateless.

To stay simple, the following code:

```php
$validator = new Boolean();
if ($validator->isValid(true)) {
// Do something
} else {
$error = $validator->getErrorMessages();
}
```

Will be transformed to:

```php
$validator = new Boolean();
$validationResult = $validator->validate($value);

if ($validationResult->isValid()) {
// Do something
} else {
$error = $validationResult->getErrorMessages();
}
```

As for input filters, validationResult instances are serializable and can be transformed to JSON.

Translation would still be handled by the validators themselves: they will translate the error messages and pass them to the validation result.

What do you think of this idea? Is there any drawback that you may see from it?