Skip to content

Commit 4529901

Browse files
authored
Merge pull request #6926 from kenjis/fix-docs-controllers
docs: improve controller validate()
2 parents 2a5dad2 + 9db8be9 commit 4529901

5 files changed

Lines changed: 38 additions & 5 deletions

File tree

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Commands
118118
Others
119119
======
120120

121+
- Added ``$this->validateData()`` in Controller. See :ref:`controller-validatedata`.
121122
- Content Security Policy (CSP) enhancements
122123
- Added the configs ``$scriptNonceTag`` and ``$styleNonceTag`` in ``Config\ContentSecurityPolicy`` to customize the CSP placeholders (``{csp-script-nonce}`` and ``{csp-style-nonce}``)
123124
- Added the config ``$autoNonce`` in ``Config\ContentSecurityPolicy`` to disable the CSP placeholder replacement

user_guide_src/source/incoming/controllers.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ modify this by passing the duration (in seconds) as the first parameter:
8484

8585
.. _controller-validate:
8686

87-
Validating data
87+
Validating Data
8888
***************
8989

9090
$this->validate()
@@ -95,6 +95,15 @@ The method accepts an array of rules in the first parameter,
9595
and in the optional second parameter, an array of custom error messages to display
9696
if the items are not valid. Internally, this uses the controller's
9797
``$this->request`` instance to get the data to be validated.
98+
99+
.. warning::
100+
The ``validate()`` method uses :ref:`Validation::withRequest() <validation-withrequest>` method.
101+
It validates data from :ref:`$request->getJSON() <incomingrequest-getting-json-data>`
102+
or :ref:`$request->getRawInput() <incomingrequest-retrieving-raw-data>`
103+
or :ref:`$request->getVar() <incomingrequest-getting-data>`.
104+
Which data is used depends on the request. Remember that an attacker is free to send any request to
105+
the server.
106+
98107
The :doc:`Validation Library docs </libraries/validation>` have details on
99108
rule and message array formats, as well as available rules:
100109

@@ -107,9 +116,13 @@ the ``$rules`` array with the name of the group as defined in **app/Config/Valid
107116

108117
.. 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.
109118

119+
.. _controller-validatedata:
120+
110121
$this->validateData()
111122
=====================
112123

124+
.. versionadded:: 4.2.0
125+
113126
Sometimes you may want to check the controller method parameters or other custom data.
114127
In that case, you can use the ``$this->validateData()`` method.
115128
The method accepts an array of data to validate in the first parameter:

user_guide_src/source/incoming/controllers/006.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public function product(int $id)
1717
];
1818

1919
if (! $this->validateData($data, $rule)) {
20-
// ...
20+
return view('store/product', [
21+
'errors' => $this->validator->getErrors(),
22+
]);
2123
}
2224

2325
// ...

user_guide_src/source/incoming/incomingrequest.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ With CodeIgniter's built-in methods you can simply do this:
8080

8181
.. literalinclude:: incomingrequest/008.php
8282

83+
.. _incomingrequest-getting-data:
84+
8385
Getting Data
8486
============
8587

8688
The ``getVar()`` method will pull from ``$_REQUEST``, so will return any data from ``$_GET``, ``$POST``, or ``$_COOKIE`` (depending on php.ini `request-order <https://www.php.net/manual/en/ini.core.php#ini.request-order>`_).
8789

88-
.. note:: If the incoming request has a ``CONTENT_TYPE`` header set to ``application/json``,
90+
.. note:: If the incoming request has a ``Content-Type`` header set to ``application/json``,
8991
the ``getVar()`` method returns the JSON data instead of ``$_REQUEST`` data.
9092

9193
While this
@@ -103,6 +105,8 @@ maintaining the ability to control the order you look for it:
103105
* ``$request->getPostGet()`` - checks ``$_POST`` first, then ``$_GET``
104106
* ``$request->getGetPost()`` - checks ``$_GET`` first, then ``$_POST``
105107

108+
.. _incomingrequest-getting-json-data:
109+
106110
Getting JSON Data
107111
=================
108112

@@ -119,7 +123,7 @@ arrays, pass in ``true`` as the first parameter.
119123
The second and third parameters match up to the ``depth`` and ``options`` arguments of the
120124
`json_decode <https://www.php.net/manual/en/function.json-decode.php>`_ PHP function.
121125

122-
If the incoming request has a ``CONTENT_TYPE`` header set to ``application/json``, you can also use ``getVar()`` to get
126+
If the incoming request has a ``Content-Type`` header set to ``application/json``, you can also use ``getVar()`` to get
123127
the JSON stream. Using ``getVar()`` in this way will always return an object.
124128

125129
Getting Specific Data from JSON
@@ -132,12 +136,14 @@ data that you want or you can use "dot" notation to dig into the JSON to get dat
132136

133137
If you want the result to be an associative array instead of an object, you can use ``getJsonVar()`` instead and pass
134138
true in the second parameter. This function can also be used if you can't guarantee that the incoming request will have the
135-
correct ``CONTENT_TYPE`` header.
139+
correct ``Content-Type`` header.
136140

137141
.. literalinclude:: incomingrequest/011.php
138142

139143
.. note:: See the documentation for :php:func:`dot_array_search()` in the ``Array`` helper for more information on "dot" notation.
140144

145+
.. _incomingrequest-retrieving-raw-data:
146+
141147
Retrieving Raw Data (PUT, PATCH, DELETE)
142148
========================================
143149

user_guide_src/source/libraries/validation.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ To give a labeled error message you can set up as:
276276

277277
.. literalinclude:: validation/007.php
278278

279+
.. _validation-withrequest:
280+
279281
withRequest()
280282
=============
281283

@@ -286,6 +288,15 @@ data to be validated:
286288

287289
.. literalinclude:: validation/008.php
288290

291+
.. note:: This method gets JSON data from
292+
:ref:`$request->getJSON() <incomingrequest-getting-json-data>`
293+
when the request is a JSON request (``Content-Type: application/json``),
294+
or gets Raw data from
295+
:ref:`$request->getRawInput() <incomingrequest-retrieving-raw-data>`
296+
when the request is a PUT, PATCH, DELETE request and
297+
is not HTML form post (``Content-Type: multipart/form-data``),
298+
or gets data from :ref:`$request->getVar() <incomingrequest-getting-data>`.
299+
289300
Working with Validation
290301
***********************
291302

0 commit comments

Comments
 (0)