Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed May 30, 2020
1 parent 52e637b commit a6f6494
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ You can now test the `ValidationResult` and throw an exception if it contains er

```php
<?php

if ($validationResult->isFailed()) {
$validationResult->setMessage('Please check your input');

throw new ValidationException($validationResult);
throw new ValidationException('Please check your input', $validationResult);
}
```

Expand Down Expand Up @@ -80,11 +77,8 @@ if (empty($data['password'])) {

// Check validation result
if ($validation->isFailed()) {
// Global error message
$validation->setMessage('Please check your input');

// Trigger error response (see validation middleware)
throw new ValidationException($validation);
throw new ValidationException('Please check your input', $validation);
}
```

Expand All @@ -103,8 +97,7 @@ $validation = new ValidationResult();
// ...

if ($validation->isFailed()) {
$validation->setMessage('Please check your input');
throw new ValidationException($validation);
throw new ValidationException('Please check your input', $validation);
}
```

Expand Down Expand Up @@ -171,7 +164,11 @@ return [
ValidationExceptionMiddleware::class => static function (ContainerInterface $container) {
$factory = $container->get(ResponseFactoryInterface::class);

return new ValidationExceptionMiddleware($factory, new ErrorDetailsResultTransformer(), new JsonEncoder());
return new ValidationExceptionMiddleware(
$factory,
new ErrorDetailsResultTransformer(),
new JsonEncoder()
);
},

ResponseFactoryInterface::class => static function (ContainerInterface $container) {
Expand Down Expand Up @@ -208,10 +205,8 @@ if (empty($data->username)) {

// Check validation result
if ($validation->isFailed()) {
$validation->setMessage('Please check your input');

// Trigger the validation middleware
throw new ValidationException($validation);
throw new ValidationException('Please check your input', $validation);
}
```

Expand All @@ -228,13 +223,16 @@ you can implement a custom transformer against the

namespace App\Transformer;

use Selective\Validation\Exception\ValidationException;
use Selective\Validation\Transformer\ResultTransformerInterface;
use Selective\Validation\ValidationResult;

final class MyValidationTransformer implements ResultTransformerInterface
{
public function transform(ValidationResult $validationResult): array
{
public function transform(
ValidationResult $validationResult,
ValidationException $exception = null
): array {
// Implement your own data structure for the response
// ...

Expand Down Expand Up @@ -296,9 +294,7 @@ if (!$this->existsEmailInDatabase($formData['email'])) {
}

if ($validationResult->isFailed()) {
$validationResult->setMessage('Validation failed. Please check your input.');

throw new ValidationException($validationResult);
throw new ValidationException('Validation failed. Please check your input.', $validationResult);
}
```

Expand Down Expand Up @@ -345,7 +341,8 @@ $validator = $this->validationFactory->createValidator();

// ...

$validationResult = $this->validationFactory->createErrorCollector()->addErrors($validator->validate($form));
$validationResult = $this->validationFactory->createErrorCollector()
->addErrors($validator->validate($form));

if ($validationResult->isFailed()) {
// ...
Expand Down

0 comments on commit a6f6494

Please sign in to comment.