@@ -203,6 +203,60 @@ Then add validation rules in the controller (**Form.php**):
203203
204204If 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*********************
207261Config for Validation
208262*********************
0 commit comments