Skip to content

Commit 71e1e43

Browse files
authored
Merge pull request #8169 from kenjis/docs-warn-validate-in-controller
docs: add warning to validate() in controller
2 parents 7f3afe9 + 04505c7 commit 71e1e43

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

user_guide_src/source/incoming/controllers.rst

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,48 @@ modify this by passing the duration (in seconds) as the first parameter:
8787
Validating Data
8888
***************
8989

90+
.. _controller-validatedata:
91+
92+
$this->validateData()
93+
=====================
94+
95+
.. versionadded:: 4.2.0
96+
97+
To simplify data checking, the controller also provides the convenience method
98+
``validateData()``.
99+
100+
The method accepts (1) an array of data to validate, (2) an array of rules,
101+
(3) an optional array of custom error messages to display if the items are not valid,
102+
(4) an optional database group to use.
103+
104+
The :doc:`Validation Library docs </libraries/validation>` have details on
105+
rule and message array formats, as well as available rules:
106+
107+
.. literalinclude:: controllers/006.php
108+
90109
.. _controller-validate:
91110

92111
$this->validate()
93112
=================
94113

95-
To simplify data checking, the controller also provides the convenience method ``validate()``.
114+
.. important:: This method exists only for backward compatibility. Do not use it
115+
in new projects. Even if you are already using it, we recommend that you use
116+
the ``validateData()`` method instead.
117+
118+
The controller also provides the convenience method ``validate()``.
119+
120+
.. warning:: Instead of ``validate()``, use ``validateData()`` to validate POST
121+
data only. ``validate()`` uses ``$request->getVar()`` which returns
122+
``$_GET``, ``$_POST`` or ``$_COOKIE`` data in that order (depending on php.ini
123+
`request-order <https://www.php.net/manual/en/ini.core.php#ini.request-order>`_).
124+
Newer values override older values. POST values may be overridden by the
125+
cookies if they have the same name.
126+
96127
The method accepts an array of rules in the first parameter,
97128
and in the optional second parameter, an array of custom error messages to display
98-
if the items are not valid. Internally, this uses the controller's
129+
if the items are not valid.
130+
131+
Internally, this uses the controller's
99132
``$this->request`` instance to get the data to be validated.
100133

101134
The :doc:`Validation Library docs </libraries/validation>` have details on
@@ -123,19 +156,6 @@ the ``$rules`` array with the name of the group as defined in **app/Config/Valid
123156

124157
.. note:: Validation can also be handled automatically in the model, but sometimes it's easier to do it in the controller. Where is up to you.
125158

126-
.. _controller-validatedata:
127-
128-
$this->validateData()
129-
=====================
130-
131-
.. versionadded:: 4.2.0
132-
133-
Sometimes you may want to check the controller method parameters or other custom data.
134-
In that case, you can use the ``$this->validateData()`` method.
135-
The method accepts an array of data to validate in the first parameter:
136-
137-
.. literalinclude:: controllers/006.php
138-
139159
Protecting Methods
140160
******************
141161

user_guide_src/source/incoming/controllers/006.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public function product(int $id)
88
{
99
$data = [
1010
'id' => $id,
11-
'name' => $this->request->getVar('name'),
11+
'name' => $this->request->getPost('name'),
1212
];
1313

1414
$rule = [

0 commit comments

Comments
 (0)