Skip to content

Commit 717a2a9

Browse files
committed
docs: Improve Validation docs
1 parent a9fa3d0 commit 717a2a9

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

user_guide_src/source/libraries/validation.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,60 @@ Then add validation rules in the controller (**Form.php**):
203203

204204
If you submit the form you should see the success page or the form with error messages.
205205

206+
****************************
207+
Working with different types
208+
****************************
209+
210+
Since the input value is not limited to scalar types, you can check various objects, enumerations or global data (see :ref:`rules-for-file-uploads`).
211+
Working with the validator is no different, you only need to add custom rules.
212+
This behavior can be useful when creating entities.
213+
214+
An example for validation can be ``DateTime`` for the creation date, ``URI`` for the profile link, ``SplFileInfo`` for the avatar file.
215+
216+
Let's check the input data before creating a new user, with a file structure:
217+
218+
.. code-block:: text
219+
220+
app/
221+
├── Config
222+
│ └── Validation.php:
223+
├── Controllers
224+
│ └── UserController.php
225+
├── Entity
226+
│ └── User.php
227+
├── Validation
228+
│ └── UserRules.php
229+
└── ValueObject
230+
└── Name.php
231+
232+
Add custom value-object ``Name``. It is required when working with the ``User``:
233+
234+
.. literalinclude:: validation/048.php
235+
:lines: 2-
236+
237+
Create rule. We want to make sure that the username will be of the specified type and have a non-empty value:
238+
239+
.. literalinclude:: validation/049.php
240+
:lines: 2-
241+
242+
Add rule to list in **app/Config/Validation.php**:
243+
244+
.. literalinclude:: validation/050.php
245+
:lines: 2-
246+
247+
We can check the data manually. It looks more efficiently with the ``Validator``:
248+
249+
250+
.. literalinclude:: validation/051.php
251+
:lines: 2-
252+
253+
.. note:: Additionally, some basic rules can be applied, such as ``required``, ``permit_empty``, ``field_exists``, ``if_exist``.
254+
255+
Try to create a ``User`` in the controller. Depending on the result, we can throw an exception or handle errors:
256+
257+
.. literalinclude:: validation/052.php
258+
:lines: 2-
259+
206260
*********************
207261
Config for Validation
208262
*********************
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\ValueObject;
4+
5+
class Name
6+
{
7+
public $value = '';
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Validation;
4+
5+
use App\ValueObject\Name;
6+
7+
class UserRules
8+
{
9+
public function valid_name($name, ?string &$error = null)
10+
{
11+
if (! $name instanceof Name) {
12+
$error = 'The name should be Name::class.';
13+
14+
return false;
15+
}
16+
17+
if ($name->value === '') {
18+
$error = 'The name should not be empty.';
19+
20+
return false;
21+
}
22+
23+
return true;
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use App\Validation\UserRules;
6+
use CodeIgniter\Config\BaseConfig;
7+
8+
class Validation extends BaseConfig
9+
{
10+
public array $ruleSets = [
11+
// ...
12+
UserRules::class,
13+
];
14+
15+
// ...
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use App\ValueObject\Name;
6+
use DomainException;
7+
8+
class User
9+
{
10+
public Name $name;
11+
public string $createdAt;
12+
13+
public function __construct(array $data = [])
14+
{
15+
if (! service('validation')->validateData($data, [
16+
'name' => 'required|valid_name',
17+
'created_at' => 'valid_date[d/m/Y]',
18+
])) {
19+
// The validation failed
20+
throw new DomainException('Invalid data for "User"');
21+
}
22+
23+
// Working with allowed data
24+
$this->name = $data['name'];
25+
$this->createdAt = $data['created_at'];
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Entity\User;
6+
use App\ValueObject\Name;
7+
use DomainException;
8+
9+
class UserController
10+
{
11+
public function index(): string
12+
{
13+
$data = [
14+
'name' => new Name('Ivan'),
15+
'created_at' => '01/01/2000',
16+
];
17+
18+
try {
19+
$user = new User($data);
20+
21+
// Hi, Ivan
22+
return 'Hi, ' . $user->name->value;
23+
} catch (DomainException $e) {
24+
echo 'Found errors: ';
25+
26+
print_r(service('validation')->getErrors());
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)