Skip to content

Commit 67323e8

Browse files
authored
Merge pull request #72 from rakit/improve-documentation
Improve documentation
2 parents 5ea3c11 + 05f2bf7 commit 67323e8

File tree

1 file changed

+120
-32
lines changed

1 file changed

+120
-32
lines changed

README.md

Lines changed: 120 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ Rakit Validation - PHP Standalone Validation Library
77

88
PHP Standalone library for validating data. Inspired by `Illuminate\Validation` Laravel.
99

10+
## Features
11+
12+
* API like Laravel validation.
13+
* Array validation.
14+
* `$_FILES` validation with multiple file support.
15+
* Custom attribute aliases.
16+
* Custom validation messages.
17+
* Custom rule.
18+
1019
## Requirements
1120

1221
* PHP 7.0 or higher
@@ -375,6 +384,48 @@ Get count messages.
375384
Check if given key has an error. It returns `bool` if a key has an error, and otherwise.
376385

377386

387+
## Getting Validated, Valid, and Invalid Data
388+
389+
For example you have validation like this:
390+
391+
```php
392+
$validation = $validator->validate([
393+
'title' => 'Lorem Ipsum',
394+
'body' => 'Lorem ipsum dolor sit amet ...',
395+
'published' => null,
396+
'something' => '-invalid-'
397+
], [
398+
'title' => 'required',
399+
'body' => 'required',
400+
'published' => 'default:1|required|in:0,1',
401+
'something' => 'required|numeric'
402+
]);
403+
```
404+
405+
You can get validated data, valid data, or invalid data using methods in example below:
406+
407+
```php
408+
$validatedData = $validation->getValidatedData();
409+
// [
410+
// 'title' => 'Lorem Ipsum',
411+
// 'body' => 'Lorem ipsum dolor sit amet ...',
412+
// 'published' => '1' // notice this
413+
// 'something' => '-invalid-'
414+
// ]
415+
416+
$validData = $validation->getValidData();
417+
// [
418+
// 'title' => 'Lorem Ipsum',
419+
// 'body' => 'Lorem ipsum dolor sit amet ...',
420+
// 'published' => '1'
421+
// ]
422+
423+
$invalidData = $validation->getInvalidData();
424+
// [
425+
// 'something' => '-invalid-'
426+
// ]
427+
```
428+
378429
## Available Rules
379430

380431
> Click to show details.
@@ -972,45 +1023,82 @@ $validation = $validator->validate($_POST, [
9721023
]);
9731024
```
9741025

975-
## Getting Validated, Valid, and Invalid Data
1026+
#### Implicit Rule
9761027

977-
For example you have validation like this:
1028+
Implicit rule is a rule that if it's invalid, then next rules will be ignored. For example if attribute didn't pass `required*` rules, mostly it's next rules will also be invalids. So to prevent our next rules messages to get collected, we make `required*` rules to be implicit.
1029+
1030+
To make your custom rule implicit, you can make `$implicit` property value to be `true`. For example:
9781031

9791032
```php
980-
$validation = $validator->validate([
981-
'title' => 'Lorem Ipsum',
982-
'body' => 'Lorem ipsum dolor sit amet ...',
983-
'published' => null,
984-
'something' => '-invalid-'
985-
], [
986-
'title' => 'required',
987-
'body' => 'required',
988-
'published' => 'default:1|required|in:0,1',
989-
'something' => 'required|numeric'
990-
]);
991-
```
1033+
<?php
9921034

993-
You can get validated data, valid data, or invalid data using methods in example below:
1035+
use Rakit\Validation\Rule;
1036+
1037+
class YourCustomRule extends Rule
1038+
{
1039+
1040+
protected $implicit = true;
1041+
1042+
}
1043+
```
1044+
1045+
#### Modify Value
1046+
1047+
In some case, you may want your custom rule to be able to modify it's attribute value like our `default/defaults` rule. So in current and next rules checks, your modified value will be used.
1048+
1049+
To do this, you should implements `Rakit\Validation\Rules\Interfaces\ModifyValue` and create method `modifyValue($value)` to your custom rule class.
1050+
1051+
For example:
9941052

9951053
```php
996-
$validatedData = $validation->getValidatedData();
997-
// [
998-
// 'title' => 'Lorem Ipsum',
999-
// 'body' => 'Lorem ipsum dolor sit amet ...',
1000-
// 'published' => '1' // notice this
1001-
// 'something' => '-invalid-'
1002-
// ]
1054+
<?php
10031055

1004-
$validData = $validation->getValidData();
1005-
// [
1006-
// 'title' => 'Lorem Ipsum',
1007-
// 'body' => 'Lorem ipsum dolor sit amet ...',
1008-
// 'published' => '1'
1009-
// ]
1056+
use Rakit\Validation\Rule;
1057+
use Rakit\Validation\Rules\Interfaces\ModifyValue;
10101058

1011-
$invalidData = $validation->getInvalidData();
1012-
// [
1013-
// 'something' => '-invalid-'
1014-
// ]
1059+
class YourCustomRule extends Rule implements ModifyValue
1060+
{
1061+
...
1062+
1063+
public function modifyValue($value)
1064+
{
1065+
// Do something with $value
1066+
1067+
return $value;
1068+
}
1069+
1070+
...
1071+
}
10151072
```
10161073

1074+
#### Before Validation Hook
1075+
1076+
You may want to do some preparation before validation running. For example our `uploaded_file` rule will resolves attribute value that come from `$_FILES` (undesirable) array structure to be well-organized array structure, so we can validate multiple file upload just like validating other data.
1077+
1078+
To do this, you should implements `Rakit\Validation\Rules\Interfaces\BeforeValidate` and create method `beforeValidate()` to your custom rule class.
1079+
1080+
For example:
1081+
1082+
```php
1083+
<?php
1084+
1085+
use Rakit\Validation\Rule;
1086+
use Rakit\Validation\Rules\Interfaces\BeforeValidate;
1087+
1088+
class YourCustomRule extends Rule implements BeforeValidate
1089+
{
1090+
...
1091+
1092+
public function beforeValidate()
1093+
{
1094+
$attribute = $this->getAttribute(); // Rakit\Validation\Attribute instance
1095+
$validation = $this->validation; // Rakit\Validation\Validation instance
1096+
1097+
// Do something with $attribute and $validation
1098+
// For example change attribute value
1099+
$validation->setValue($attribute->getKey(), "your custom value");
1100+
}
1101+
1102+
...
1103+
}
1104+
```

0 commit comments

Comments
 (0)