Skip to content

Commit 98ed457

Browse files
committed
docs: add warning to validate()
1 parent 7f3afe9 commit 98ed457

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

‎user_guide_src/source/incoming/controllers.rst‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,44 @@ 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 an array of data to validate in the first parameter, and an
101+
array of rules in the second parameter, and in the optional third parameter,
102+
an array of custom error messages to display if the items are not valid.
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+
The controller also provides the convenience method ``validate()``.
115+
116+
.. warning:: Instead of ``validate()``, use ``validateData()`` to validate POST
117+
data only. ``validate()`` uses ``$request->getVar()`` which returns
118+
``$_GET``, ``$_POST`` or ``$_COOKIE`` data in that order (depending on php.ini
119+
`request-order <https://www.php.net/manual/en/ini.core.php#ini.request-order>`_).
120+
Newer values override older values. POST values may be overridden by the
121+
cookies if they have the same name.
122+
96123
The method accepts an array of rules in the first parameter,
97124
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
125+
if the items are not valid.
126+
127+
Internally, this uses the controller's
99128
``$this->request`` instance to get the data to be validated.
100129

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

124153
.. 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.
125154

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-
139155
Protecting Methods
140156
******************
141157

0 commit comments

Comments
 (0)