Skip to content

Commit 8a56e73

Browse files
committed
Add documentation to new 'defaults' rule, and getValidatedData, getValidData, and getInvalidData.
1 parent 9e252c0 commit 8a56e73

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Below is list of all available validation rules
270270
* [required_with_all](#rule-required_with_all)
271271
* [required_without_all](#rule-required_without_all)
272272
* [uploaded_file](#rule-uploaded_file)
273+
* [default/defaults](#rule-default)
273274
* [email](#rule-email)
274275
* [alpha](#rule-alpha)
275276
* [numeric](#rule-numeric)
@@ -365,7 +366,25 @@ Here are some example definitions and explanations:
365366
* `uploaded_file:0,1M`: uploaded file size must be between 0 - 1 MB, but uploaded file is optional.
366367
* `required|uploaded_file:0,1M,png,jpeg`: uploaded file size must be between 0 - 1MB and mime types must be `image/jpeg` or `image/png`.
367368

369+
<a id="rule-default"></a>
370+
#### default/defaults
368371

372+
This is special rule that doesn't validate anything. It just set default value to your attribute.
373+
374+
For example if you have validation like this
375+
376+
```php
377+
$validation = $validator->validate([
378+
'enabled' => null
379+
], [
380+
'enabled' => 'default:1|required|in:0,1'
381+
'published' => 'default:0|required|in:0,1'
382+
]);
383+
384+
$validation->passes(); // true
385+
```
386+
387+
Validation passes because we sets default value for `enabled` and `published` to `1` and `0` which is valid.
369388

370389
<a id="rule-email"></a>
371390
#### email
@@ -672,3 +691,46 @@ $validation = $validator->validate($_POST, [
672691
]
673692
]);
674693
```
694+
695+
## Getting Validated, Valid, and Invalid Data
696+
697+
For example you have validation like this:
698+
699+
```php
700+
$validation = $validator->validate([
701+
'title' => 'Lorem Ipsum',
702+
'body' => 'Lorem ipsum dolor sit amet ...',
703+
'published' => null,
704+
'something' => '-invalid-'
705+
], [
706+
'title' => 'required',
707+
'body' => 'required',
708+
'published' => 'default:1|required|in:0,1',
709+
'something' => 'required|numeric'
710+
]);
711+
```
712+
713+
You can get validated data, valid data, or invalid data using methods in example below:
714+
715+
```php
716+
$validatedData = $validation->getValidatedData();
717+
// [
718+
// 'title' => 'Lorem Ipsum',
719+
// 'body' => 'Lorem ipsum dolor sit amet ...',
720+
// 'published' => '1' // notice this
721+
// 'something' => '-invalid-'
722+
// ]
723+
724+
$validData = $validation->getValidData();
725+
// [
726+
// 'title' => 'Lorem Ipsum',
727+
// 'body' => 'Lorem ipsum dolor sit amet ...',
728+
// 'published' => '1'
729+
// ]
730+
731+
$invalidData = $validation->getInvalidData();
732+
// [
733+
// 'something' => '-invalid-'
734+
// ]
735+
```
736+

0 commit comments

Comments
 (0)