Skip to content

Commit b11e3d3

Browse files
committed
Add implicit rule, modify value, and before validate guides
1 parent 6dcd405 commit b11e3d3

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,87 @@ $validation = $validator->validate($_POST, [
981981
]);
982982
```
983983

984+
#### Implicit Rule
985+
986+
Implicit rule is a rule that if it's invalid, then next rules will be ignored.
987+
For example `required*` rules, if some attribute didn't pass `required*` rules, 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.
988+
989+
To make your custom rule implicit, you can make `$implicit` property value to be `true`. For example:
990+
991+
```php
992+
<?php
993+
994+
use Rakit\Validation\Rule;
995+
996+
class YourCustomRule extends Rule
997+
{
998+
999+
protected $implicit = true;
1000+
1001+
}
1002+
```
1003+
1004+
#### Modify Value
1005+
1006+
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.
1007+
1008+
To do this, you should implements `Rakit\Validation\Rules\Interfaces\ModifyValue` and create method `modifyValue($value)` to your custom rule class.
1009+
1010+
For example:
1011+
1012+
```php
1013+
<?php
1014+
1015+
use Rakit\Validation\Rule;
1016+
use Rakit\Validation\Rules\Interfaces\ModifyValue;
1017+
1018+
class YourCustomRule extends Rule implements ModifyValue
1019+
{
1020+
...
1021+
1022+
public function modifyValue($value)
1023+
{
1024+
// Do something with $value
1025+
1026+
return $value;
1027+
}
1028+
1029+
...
1030+
}
1031+
```
1032+
1033+
#### Before Validation Hook
1034+
1035+
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.
1036+
1037+
To do this, you should implements `Rakit\Validation\Rules\Interfaces\BeforeValidate` and create method `beforeValidate()` to your custom rule class.
1038+
1039+
For example:
1040+
1041+
```php
1042+
<?php
1043+
1044+
use Rakit\Validation\Rule;
1045+
use Rakit\Validation\Rules\Interfaces\BeforeValidate;
1046+
1047+
class YourCustomRule extends Rule implements BeforeValidate
1048+
{
1049+
...
1050+
1051+
public function beforeValidate()
1052+
{
1053+
$attribute = $this->getAttribute(); // Rakit\Validation\Attribute instance
1054+
$validation = $this->validation; // Rakit\Validation\Validation instance
1055+
1056+
// Do something with $attribute and $validation
1057+
// For example change attribute value
1058+
$validation->setValue($attribute->getKey(), "your custom value");
1059+
}
1060+
1061+
...
1062+
}
1063+
```
1064+
9841065
## Getting Validated, Valid, and Invalid Data
9851066

9861067
For example you have validation like this:

0 commit comments

Comments
 (0)