Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ErrorsAware interface and a base trait implementation #528

Open
xepozz opened this issue Feb 1, 2023 · 0 comments
Open

Introduce ErrorsAware interface and a base trait implementation #528

xepozz opened this issue Feb 1, 2023 · 0 comments

Comments

@xepozz
Copy link
Contributor

xepozz commented Feb 1, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants