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
| textOnly | should be text only, no spaces allowed |
15
-
| validUsername | must only contain characters 0-9, A-Z and _|
16
-
| username | alias for validUsername |
17
-
| email | must be a valid email |
18
-
| noSpaces | can't contain any spaces |
19
-
| max | max length of a string (requires arguments) |
20
-
| min | min length of a string (requires arguments) |
21
-
| date | string should be a valid date |
9
+
| Rule | Description |
10
+
| --- | --- |
11
+
|`email`| The field under validation must be formatted as an e-mail address. |
12
+
|`string`| The field under validation must contain only alphabetic characters and spaces. |
13
+
|`text`| The field under validation must contain only alphabetic characters and spaces. |
14
+
|`textOnly`| The field under validation must contain only alphabetic characters (no-spaces). |
15
+
|`alpha`| The field under validation must contain only alphabetic characters. |
16
+
|`alphaNum`| The field under validation must contain only alpha-numeric characters. |
17
+
|`alphaDash`| The field under validation must contain only alpha-numeric characters, underscores, and dashes. |
18
+
|`username`| The field under validation must contain only alpha-numeric characters and underscores. |
19
+
|`number`| The field under validation must contain only numeric characters. |
20
+
|`float`| The field under validation must contain only float values. |
21
+
|`date`| The field under validation must be a valid date. |
22
+
|`min`| The field under validation must have a minimum value. |
23
+
|`max`| The field under validation must have a maximum value. |
24
+
|`between`| The field under validation must be between two values in length. |
25
+
|`match`| The field under validation must match a value. |
26
+
|`contains`| The field under validation must contain a value. |
27
+
|`in`| The field under validation must be included in a given list of values. |
28
+
|`ip`| The field under validation must be a valid IP address. |
29
+
|`ipv4`| The field under validation must be a valid IPv4 address. |
30
+
|`ipv6`| The field under validation must be a valid IPv6 address. |
31
+
|`url`| The field under validation must be a valid URL. |
32
+
|`domain`| The field under validation must be a valid domain. |
33
+
|`creditCard`| The field under validation must be a valid credit card number. |
34
+
|`phone`| The field under validation must be a valid phone number. |
35
+
|`uuid`| The field under validation must be a valid UUID. |
36
+
|`slug`| The field under validation must be a valid slug. |
37
+
|`json`| The field under validation must be a valid JSON string. |
38
+
|`regex`| The field under validation must match a given regular expression. |
22
39
23
40
::: tip Note
24
41
These rules are **NOT** case-sensitive, so you can type them anyway you prefer, as long as the spelling is the same.
@@ -48,13 +65,15 @@ This is the data we're passing into our app.
48
65
require __DIR__ . '/vendor/autoload.php';
49
66
50
67
app()->get('/', function () {
51
-
$isValid = request()->validate([
68
+
$validatedData = request()->validate([
52
69
'email' => 'email',
53
-
'name' => 'text',
70
+
'name' => 'string',
71
+
'city' => 'string',
72
+
'country' => 'string',
54
73
]);
55
74
56
75
response()->json(
57
-
$isValid ? 'success' : request()->errors()
76
+
$validatedData ?: request()->errors()
58
77
);
59
78
});
60
79
@@ -63,32 +82,40 @@ app()->run();
63
82
64
83
</div>
65
84
66
-
The array tells the `validate()` function what rule(s) to run against what data. The `email` rule is run against the email field we passed to make sure it's a valid email. In the same way, we're running the `text` method against the name field to make sure that it only contains text and spaces. You can try this out in the editor.
85
+
The array tells the `validate()` function what rule(s) to run against what data. The `email` rule is run against the email field we passed to make sure it's a valid email. It works the same for all other fields, but there are a few things to note about Leaf's validation.
67
86
68
-
If you want the validation to fail, you can edit the `data` in the `request.json` file with invalid data. Try passing a number into the email field and see what happens.
87
+
1. The `validate()` method will only return the fields that were validated. If there are fields that weren't validated, they won't be returned. *You can try this by removing the `name` field from the `validate()` method and see what happens.*
88
+
2. The `validate()` method will return the validated data if the validation passes. If the validation fails, it will return `false`. *You can try this by passing an invalid email address and see what happens.*
89
+
3. Leaf's validation assumes that every field is required. If you want to make a field optional, you can use the `optional` rule.
69
90
70
91
## Multiple validation rules
71
92
72
93
So far, we've only looked at validating data against a single rule. But what if we want to validate data against multiple rules? We can do this by passing an array of rules into the `validate` function. For example, we can validate the email field against the `email` and `required` rules.
73
94
74
95
```php
75
-
'email' => ['required', 'email'],
96
+
'email' => ['string', 'email'],
76
97
```
77
98
78
-
You can add as many rules as you want to the array, in the order you want to validate the data. The only rule here is to make sure your validation rules don't fight each other. For example, if you use the `textOnly` rule and the `number` rule together, the validation will always fail because the `textOnly` rule will always fail the `number` rule.
99
+
You can also use a string with rules separated by a pipe `|`.
100
+
101
+
```php
102
+
'email' => 'string|email',
103
+
```
104
+
105
+
You can add as many rules as you want, in the order you want to validate the data. The only rule here is to make sure your validation rules don't fight each other. For example, if you use the `textOnly` rule and the `number` rule together, the validation will always fail because the `textOnly` rule will always fail the `number` rule and vice versa.
79
106
80
107
## Getting validation errors
81
108
82
109
When validation fails, Leaf automatically stores the errors in the request object and returns `false`. You can get these errors by calling the `errors()` method on the request object. This method returns an array of errors. You can output these errors to the user or use them in any way you want.
0 commit comments