Skip to content

Commit 9fd541a

Browse files
authored
Update validation.md (SpartnerNL#120)
1 parent 309c03e commit 9fd541a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

3.1/imports/validation.md

+25
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,31 @@ class UsersImport implements ToCollection
363363
}
364364
```
365365

366+
## Configuring the validator
367+
If you want to add conditional validation or complex validation that cannot be expressed through rules you can configure the validator similar to how you would do this with a [Form request](https://laravel.com/docs/master/validation#form-request-validation)
368+
369+
:::tip Manual validation
370+
You can use `$validator->getData()` to get access to the data under validation
371+
:::
372+
373+
```php
374+
class UsersImport implements WithValidation
375+
{
376+
public function withValidator($validator)
377+
{
378+
$validator->after(function ($validator) {
379+
if ($this->somethingElseIsInvalid()) {
380+
$validator->errors()->add('field', 'Something is wrong with this field!');
381+
}
382+
});
383+
384+
// or...
385+
386+
$validator->sometimes('*.email', 'required', $this->someConditionalRequirement());
387+
}
388+
}
389+
```
390+
366391
:::tip Validation rules
367392
For a list of all validation rules, please refer to the [Laravel document](https://laravel.com/docs/master/validation#available-validation-rules).
368393
:::

0 commit comments

Comments
 (0)