Skip to content

Commit 3d11422

Browse files
committed
fix: update tutorial
1 parent c147529 commit 3d11422

File tree

4 files changed

+58
-31
lines changed

4 files changed

+58
-31
lines changed

src/tutorial/src/step-1/description.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ Leaf offers two API styles: functional mode and class mode. This tutorial is des
3636
3737
::: -->
3838

39-
Ready to begin your journey? Click **"Next"** to get started.
39+
Ready to begin your journey? Click **"->"** to get started.

src/tutorial/src/step-15/description.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ As a next step, you can:
88

99
- Every feature you need is available as a module, so be sure to checkout the [modules documentation](/docs/modules).
1010

11-
- Join the [Leaf Discord server](https://discord.gg/SpmNs4S) to ask questions, share what you're building, and get help from the community.
11+
- Join the [Leaf Discord server](https://discord.gg/Pkrm9NJPE3) to ask questions, share what you're building, and get help from the community.
1212

1313
- Follow Leaf on [Twitter](https://twitter.com/leafphp) to stay up-to-date with the news and updates.
1414

src/tutorial/src/step-8/_hint/index/functionalMode.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
require __DIR__ . '/vendor/autoload.php';
44

55
app()->get('/', function () {
6-
$isValid = form()->validate([
7-
'name' => 'text',
8-
'country' => ['required', 'text'],
9-
'city' => 'required',
6+
$validatedData = request()->validate([
7+
'name' => 'string',
8+
'country' => ['string', 'min:2'],
9+
'city' => 'string',
1010
'email' => 'email',
1111
]);
1212

1313
response()->json(
14-
$isValid ? 'success' : form()->errors()
14+
$validatedData ?: request()->errors()
1515
);
1616
});
1717

src/tutorial/src/step-8/description.md

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,36 @@ A golden rule in web development is to never trust user input. Users will always
66

77
These are the rules Leaf provides out-of-the-box for validating data:
88

9-
| Validation rule | Purpose |
10-
|:--------------------|:---------------------------------------------|
11-
| required | field is required |
12-
| number | must only contain numbers |
13-
| text | must only contain text and spaces |
14-
| 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. |
2239

2340
::: tip Note
2441
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.
4865
require __DIR__ . '/vendor/autoload.php';
4966
5067
app()->get('/', function () {
51-
$isValid = request()->validate([
68+
$validatedData = request()->validate([
5269
'email' => 'email',
53-
'name' => 'text',
70+
'name' => 'string',
71+
'city' => 'string',
72+
'country' => 'string',
5473
]);
5574
5675
response()->json(
57-
$isValid ? 'success' : request()->errors()
76+
$validatedData ?: request()->errors()
5877
);
5978
});
6079
@@ -63,32 +82,40 @@ app()->run();
6382

6483
</div>
6584

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.
6786

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.
6990

7091
## Multiple validation rules
7192

7293
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.
7394

7495
```php
75-
'email' => ['required', 'email'],
96+
'email' => ['string', 'email'],
7697
```
7798

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.
79106

80107
## Getting validation errors
81108

82109
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.
83110

84111
```php
85-
$isValid = request()->validate([
86-
'email' => ['required', 'email'],
87-
'name' => ['required', 'text'],
112+
$validatedData = request()->validate([
113+
'email' => 'email',
114+
'name' => 'string',
88115
]);
89116

90117
response()->json(
91-
$isValid ? 'success' : request()->errors()
118+
$validatedData ?: request()->errors()
92119
);
93120
```
94121

0 commit comments

Comments
 (0)