Skip to content

Commit a6f6494

Browse files
committed
Update readme
1 parent 52e637b commit a6f6494

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ You can now test the `ValidationResult` and throw an exception if it contains er
4343

4444
```php
4545
<?php
46-
4746
if ($validationResult->isFailed()) {
48-
$validationResult->setMessage('Please check your input');
49-
50-
throw new ValidationException($validationResult);
47+
throw new ValidationException('Please check your input', $validationResult);
5148
}
5249
```
5350

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

8178
// Check validation result
8279
if ($validation->isFailed()) {
83-
// Global error message
84-
$validation->setMessage('Please check your input');
85-
8680
// Trigger error response (see validation middleware)
87-
throw new ValidationException($validation);
81+
throw new ValidationException('Please check your input', $validation);
8882
}
8983
```
9084

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

10599
if ($validation->isFailed()) {
106-
$validation->setMessage('Please check your input');
107-
throw new ValidationException($validation);
100+
throw new ValidationException('Please check your input', $validation);
108101
}
109102
```
110103

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

174-
return new ValidationExceptionMiddleware($factory, new ErrorDetailsResultTransformer(), new JsonEncoder());
167+
return new ValidationExceptionMiddleware(
168+
$factory,
169+
new ErrorDetailsResultTransformer(),
170+
new JsonEncoder()
171+
);
175172
},
176173

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

209206
// Check validation result
210207
if ($validation->isFailed()) {
211-
$validation->setMessage('Please check your input');
212-
213208
// Trigger the validation middleware
214-
throw new ValidationException($validation);
209+
throw new ValidationException('Please check your input', $validation);
215210
}
216211
```
217212

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

229224
namespace App\Transformer;
230225

226+
use Selective\Validation\Exception\ValidationException;
231227
use Selective\Validation\Transformer\ResultTransformerInterface;
232228
use Selective\Validation\ValidationResult;
233229

234230
final class MyValidationTransformer implements ResultTransformerInterface
235231
{
236-
public function transform(ValidationResult $validationResult): array
237-
{
232+
public function transform(
233+
ValidationResult $validationResult,
234+
ValidationException $exception = null
235+
): array {
238236
// Implement your own data structure for the response
239237
// ...
240238

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

298296
if ($validationResult->isFailed()) {
299-
$validationResult->setMessage('Validation failed. Please check your input.');
300-
301-
throw new ValidationException($validationResult);
297+
throw new ValidationException('Validation failed. Please check your input.', $validationResult);
302298
}
303299
```
304300

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

346342
// ...
347343

348-
$validationResult = $this->validationFactory->createErrorCollector()->addErrors($validator->validate($form));
344+
$validationResult = $this->validationFactory->createErrorCollector()
345+
->addErrors($validator->validate($form));
349346

350347
if ($validationResult->isFailed()) {
351348
// ...

0 commit comments

Comments
 (0)