@@ -32,8 +32,8 @@ composer require "rakit/validation"
32
32
33
33
#### Usage
34
34
35
- There are two ways to validating data with this library. Using ` make ` to make validation object,
36
- then validate it using ` validate ` . Or just use ` validate ` .
35
+ There are two ways to validating data with this library. Using ` make ` to make validation object,
36
+ then validate it using ` validate ` . Or just use ` validate ` .
37
37
Examples:
38
38
39
39
Using ` make ` :
@@ -112,7 +112,7 @@ if ($validation->fails()) {
112
112
113
113
```
114
114
115
- In this case, 2 examples above will output the same results.
115
+ In this case, 2 examples above will output the same results.
116
116
117
117
But with ` make ` you can setup something like custom invalid message, custom attribute alias, etc before validation running.
118
118
@@ -234,7 +234,7 @@ $validation_a->validate();
234
234
235
235
#### Custom Message for Specific Attribute Rule
236
236
237
- Sometimes you may want to set custom message for specific rule attribute.
237
+ Sometimes you may want to set custom message for specific rule attribute.
238
238
To do this you can use ` : ` as message separator or using chaining methods.
239
239
240
240
Examples:
@@ -270,12 +270,12 @@ $validation_a->validate();
270
270
271
271
## Translation
272
272
273
- Translation is different with custom messages.
273
+ Translation is different with custom messages.
274
274
Translation may needed when you use custom message for rule ` in ` , ` not_in ` , ` mimes ` , and ` uploaded_file ` .
275
275
276
- For example if you use rule ` in:1,2,3 ` we will set invalid message like "The Attribute only allows '1', '2', or '3'"
276
+ For example if you use rule ` in:1,2,3 ` we will set invalid message like "The Attribute only allows '1', '2', or '3'"
277
277
where part "'1', '2', or '3'" is comes from ": allowed_values " tag.
278
- So if you have custom Indonesian message ": attribute hanya memperbolehkan : allowed_values ",
278
+ So if you have custom Indonesian message ": attribute hanya memperbolehkan : allowed_values ",
279
279
we will set invalid message like "Attribute hanya memperbolehkan '1', '2', or '3'" which is the "or" word is not part of Indonesian language.
280
280
281
281
So, to solve this problem, we can use translation like this:
@@ -295,7 +295,7 @@ $validation = $validator->validate($inputs, [
295
295
'nomor' => 'in:1,2,3'
296
296
]);
297
297
298
- $message = $validation->errors()->first('nomor'); // "Nomor hanya memperbolehkan '1', '2', atau '3'"
298
+ $message = $validation->errors()->first('nomor'); // "Nomor hanya memperbolehkan '1', '2', atau '3'"
299
299
```
300
300
301
301
> Actually, our built-in rules only use words 'and' and 'or' that you may need to translates.
@@ -319,7 +319,7 @@ Get all messages as flatten array.
319
319
Examples:
320
320
321
321
``` php
322
- $messages = $errors->all();
322
+ $messages = $errors->all();
323
323
// [
324
324
// 'Email is not valid email',
325
325
// 'Password minimum 6 character',
@@ -341,7 +341,7 @@ Get only first message from all existing keys.
341
341
Examples:
342
342
343
343
``` php
344
- $messages = $errors->firstOfAll();
344
+ $messages = $errors->firstOfAll();
345
345
// [
346
346
// 'email' => Email is not valid email',
347
347
// 'password' => 'Password minimum 6 character',
@@ -354,13 +354,13 @@ $messages = $errors->firstOfAll('<li>:message</li>');
354
354
// ]
355
355
```
356
356
357
- Argument ` $dotNotation ` is for array validation.
357
+ Argument ` $dotNotation ` is for array validation.
358
358
If it is ` false ` it will return original array structure, if it ` true ` it will return flatten array with dot notation keys.
359
359
360
360
For example:
361
361
362
362
``` php
363
- $messages = $errors->firstOfAll(':message', false);
363
+ $messages = $errors->firstOfAll(':message', false);
364
364
// [
365
365
// 'contacts' => [
366
366
// 1 => [
@@ -470,7 +470,7 @@ The field under this validation must be present and not 'empty'.
470
470
Here are some examples:
471
471
472
472
| Value | Valid |
473
- | --------------- | ------- |
473
+ | ------------- | ----- |
474
474
| ` 'something' ` | true |
475
475
| ` '0' ` | true |
476
476
| ` 0 ` | true |
@@ -524,18 +524,18 @@ The field under validation must be present and not empty only when all of the ot
524
524
525
525
<details ><summary ><strong >uploaded_file</strong >:min_size,max_size,extension_a,extension_b,...</summary >
526
526
527
- This rule will validate data from ` $_FILES ` .
527
+ This rule will validate data from ` $_FILES ` .
528
528
Field under this rule must be follows rules below to be valid:
529
529
530
- * ` $_FILES['key']['error'] ` must be ` UPLOAD_ERR_OK ` or ` UPLOAD_ERR_NO_FILE ` . For ` UPLOAD_ERR_NO_FILE ` you can validate it with ` required ` rule.
530
+ * ` $_FILES['key']['error'] ` must be ` UPLOAD_ERR_OK ` or ` UPLOAD_ERR_NO_FILE ` . For ` UPLOAD_ERR_NO_FILE ` you can validate it with ` required ` rule.
531
531
* If min size is given, uploaded file size ** MUST NOT** be lower than min size.
532
532
* If max size is given, uploaded file size ** MUST NOT** be higher than max size.
533
533
* If file types is given, mime type must be one of those given types.
534
534
535
535
Here are some example definitions and explanations:
536
536
537
- * ` uploaded_file ` : uploaded file is optional. When it is not empty, it must be ` ERR_UPLOAD_OK ` .
538
- * ` required|uploaded_file ` : uploaded file is required, and it must be ` ERR_UPLOAD_OK ` .
537
+ * ` uploaded_file ` : uploaded file is optional. When it is not empty, it must be ` ERR_UPLOAD_OK ` .
538
+ * ` required|uploaded_file ` : uploaded file is required, and it must be ` ERR_UPLOAD_OK ` .
539
539
* ` uploaded_file:0,1M ` : uploaded file size must be between 0 - 1 MB, but uploaded file is optional.
540
540
* ` 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 ` .
541
541
@@ -600,7 +600,7 @@ The `$_FILES` item under validation must have a MIME type corresponding to one o
600
600
601
601
<details ><summary ><strong >default/defaults</strong ></summary >
602
602
603
- This is special rule that doesn't validate anything.
603
+ This is special rule that doesn't validate anything.
604
604
It just set default value to your attribute if that attribute is empty or not present.
605
605
606
606
For example if you have validation like this
@@ -684,15 +684,15 @@ The field under this rule may have alpha characters, as well as spaces.
684
684
685
685
The field under this rule must be included in the given list of values.
686
686
687
- This rule is using ` in_array ` to check the value.
688
- By default ` in_array ` disable strict checking.
687
+ This rule is using ` in_array ` to check the value.
688
+ By default ` in_array ` disable strict checking.
689
689
So it doesn't check data type.
690
690
If you want enable strict checking, you can invoke validator like this:
691
691
692
692
``` php
693
693
$validation = $validator->validate($data, [
694
694
'enabled' => [
695
- 'required',
695
+ 'required',
696
696
$validator('in', [true, 1])->strict()
697
697
]
698
698
]);
@@ -712,7 +712,7 @@ This rule also using `in_array`. You can enable strict checking by invoking vali
712
712
713
713
<details ><summary ><strong >min</strong >:number</summary >
714
714
715
- The field under this rule must have a size greater or equal than the given number.
715
+ The field under this rule must have a size greater or equal than the given number.
716
716
717
717
For string value, size corresponds to the number of characters. For integer or float value, size corresponds to its numerical value. For an array, size corresponds to the count of the array. If your value is numeric string, you can put ` numeric ` rule to treat its size by numeric value instead of number of characters.
718
718
@@ -731,7 +731,7 @@ $validation = $validator->validate([
731
731
732
732
<details ><summary ><strong >max</strong >:number</summary >
733
733
734
- The field under this rule must have a size lower or equal than the given number.
734
+ The field under this rule must have a size lower or equal than the given number.
735
735
Value size calculated in same way like ` min ` rule.
736
736
737
737
You can also validate uploaded file using this rule to validate maximum size of uploaded file.
@@ -749,7 +749,7 @@ $validation = $validator->validate([
749
749
750
750
<details ><summary ><strong >between</strong >:min,max</summary >
751
751
752
- The field under this rule must have a size between min and max params.
752
+ The field under this rule must have a size between min and max params.
753
753
Value size calculated in same way like ` min ` and ` max ` rule.
754
754
755
755
You can also validate uploaded file using this rule to validate size of uploaded file.
@@ -797,7 +797,7 @@ $validation = $validator->validate($inputs, [
797
797
]);
798
798
```
799
799
800
- > For common URL scheme and mailto, we combine ` FILTER_VALIDATE_URL ` to validate URL format and ` preg_match ` to validate it's scheme.
800
+ > For common URL scheme and mailto, we combine ` FILTER_VALIDATE_URL ` to validate URL format and ` preg_match ` to validate it's scheme.
801
801
Except for JDBC URL, currently it just check a valid JDBC scheme.
802
802
803
803
</details >
@@ -807,6 +807,12 @@ The field under t rule must be integer.
807
807
808
808
</details >
809
809
810
+ <details ><summary ><strong >boolean</strong ></summary >
811
+
812
+ The field under this rule must be boolean. Accepted input are ` true ` , ` false ` , ` 1 ` , ` 0 ` , ` "1" ` , and ` "0" ` .
813
+
814
+ </details >
815
+
810
816
<details ><summary ><strong >ip</strong ></summary >
811
817
812
818
The field under this rule must be valid ipv4 or ipv6.
@@ -911,7 +917,7 @@ $validation = $validator->validate($_POST, [
911
917
]);
912
918
```
913
919
914
- You can set invalid message by returning a string.
920
+ You can set invalid message by returning a string.
915
921
For example, example above would be:
916
922
917
923
``` php
@@ -931,7 +937,7 @@ $validation = $validator->validate($_POST, [
931
937
]);
932
938
```
933
939
934
- > Note: ` Rakit\Validation\Rules\Callback ` instance is binded into your Closure.
940
+ > Note: ` Rakit\Validation\Rules\Callback ` instance is binded into your Closure.
935
941
So you can access rule properties and methods using ` $this ` .
936
942
937
943
</details >
@@ -944,10 +950,10 @@ Field under this rule may be empty.
944
950
945
951
## Register/Override Rule
946
952
947
- Another way to use custom validation rule is to create a class extending ` Rakit\Validation\Rule ` .
953
+ Another way to use custom validation rule is to create a class extending ` Rakit\Validation\Rule ` .
948
954
Then register it using ` setValidator ` or ` addValidator ` .
949
955
950
- For example, you want to create ` unique ` validator that check field availability from database.
956
+ For example, you want to create ` unique ` validator that check field availability from database.
951
957
952
958
First, lets create ` UniqueRule ` class:
953
959
@@ -959,36 +965,36 @@ use Rakit\Validation\Rule;
959
965
class UniqueRule extends Rule
960
966
{
961
967
protected $message = ":attribute :value has been used";
962
-
968
+
963
969
protected $fillableParams = ['table', 'column', 'except'];
964
-
970
+
965
971
protected $pdo;
966
-
972
+
967
973
public function __construct(PDO $pdo)
968
974
{
969
975
$this->pdo = $pdo;
970
976
}
971
-
977
+
972
978
public function check($value): bool
973
979
{
974
980
// make sure required parameters exists
975
981
$this->requireParameters(['table', 'column']);
976
-
982
+
977
983
// getting parameters
978
984
$column = $this->parameter('column');
979
985
$table = $this->parameter('table');
980
986
$except = $this->parameter('except');
981
-
987
+
982
988
if ($except AND $except == $value) {
983
989
return true;
984
990
}
985
-
991
+
986
992
// do query
987
993
$stmt = $this->pdo->prepare("select count(*) as count from `{$table}` where `{$column}` = :value");
988
994
$stmt->bindParam(':value', $value);
989
995
$stmt->execute();
990
996
$data = $stmt->fetch(PDO::FETCH_ASSOC);
991
-
997
+
992
998
// true for valid, false for invalid
993
999
return intval($data['count']) === 0;
994
1000
}
@@ -1022,7 +1028,7 @@ $params['column'] = 'email';
1022
1028
$params['except'] = 'exception@mail.com';
1023
1029
```
1024
1030
1025
- > If you want your custom rule accept parameter list like ` in ` ,` not_in ` , or ` uploaded_file ` rules,
1031
+ > If you want your custom rule accept parameter list like ` in ` ,` not_in ` , or ` uploaded_file ` rules,
1026
1032
you just need to override ` fillParameters(array $params) ` method in your custom rule class.
1027
1033
1028
1034
Note that ` unique ` rule that we created above also can be used like this:
@@ -1046,25 +1052,25 @@ use Rakit\Validation\Rule;
1046
1052
class UniqueRule extends Rule
1047
1053
{
1048
1054
...
1049
-
1055
+
1050
1056
public function table($table)
1051
1057
{
1052
1058
$this->params['table'] = $table;
1053
1059
return $this;
1054
1060
}
1055
-
1061
+
1056
1062
public function column($column)
1057
1063
{
1058
1064
$this->params['column'] = $column;
1059
1065
return $this;
1060
1066
}
1061
-
1067
+
1062
1068
public function except($value)
1063
1069
{
1064
1070
$this->params['except'] = $value;
1065
1071
return $this;
1066
1072
}
1067
-
1073
+
1068
1074
...
1069
1075
}
1070
1076
@@ -1098,11 +1104,11 @@ class YourCustomRule extends Rule
1098
1104
protected $implicit = true;
1099
1105
1100
1106
}
1101
- ```
1107
+ ```
1102
1108
1103
1109
#### Modify Value
1104
1110
1105
- 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.
1111
+ 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.
1106
1112
1107
1113
To do this, you should implements ` Rakit\Validation\Rules\Interfaces\ModifyValue ` and create method ` modifyValue($value) ` to your custom rule class.
1108
1114
0 commit comments