validator
validator copied to clipboard
Introduce ErrorsAware interface and a base trait implementation
ErrorsAwareInterface is a marker same as PostValidationHookInterface.
It may set all errors after validation into an validated object. It may unify forms and request-model.
interface ErrorsAwareInterface {
public function addError(string $attribute): void;
public function getError(string $attribute): mixed; <-- either Stringable or Stringable[]
public function hasError(string $attribute): bool;
public function setErrors(array $errorsMap): void;
public function getErrors(): array;
public function hasErrors(): bool;
}
So the base trait should implement all of these methods;
Usage is simple:
$validator->validate($object);
if ($object->hasErrors()) {
// ...
}
Instead of
$result = $validator->validate($object);
if ($result->hasErrors()) {
// ...
}
It may be useful with nested structures that may be hard accessible and just a shortcut to pass both errors and the object somewhere together.