You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
978
1031
979
1032
```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
992
1034
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:
994
1052
995
1053
```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
1003
1055
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;
1010
1058
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
+
}
1015
1072
```
1016
1073
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
0 commit comments