Skip to content

Commit fa6494f

Browse files
authored
Merge pull request #5639 from kenjis/add-validateData
feat: add Controller::validateData()
2 parents 968c027 + 9295bbe commit fa6494f

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

system/Controller.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,37 @@ protected function loadHelpers()
128128
}
129129

130130
/**
131-
* A shortcut to performing validation on input data. If validation
132-
* is not successful, a $errors property will be set on this class.
131+
* A shortcut to performing validation on Request data.
133132
*
134133
* @param array|string $rules
135134
* @param array $messages An array of custom error messages
136135
*/
137136
protected function validate($rules, array $messages = []): bool
137+
{
138+
$this->setValidator($rules, $messages);
139+
140+
return $this->validator->withRequest($this->request)->run();
141+
}
142+
143+
/**
144+
* A shortcut to performing validation on any input data.
145+
*
146+
* @param array $data The data to validate
147+
* @param array|string $rules
148+
* @param array $messages An array of custom error messages
149+
* @param string|null $dbGroup The database group to use
150+
*/
151+
protected function validateData(array $data, $rules, array $messages = [], ?string $dbGroup = null): bool
152+
{
153+
$this->setValidator($rules, $messages);
154+
155+
return $this->validator->run($data, null, $dbGroup);
156+
}
157+
158+
/**
159+
* @param array|string $rules
160+
*/
161+
private function setValidator($rules, array $messages): void
138162
{
139163
$this->validator = Services::validation();
140164

@@ -157,6 +181,6 @@ protected function validate($rules, array $messages = []): bool
157181
$rules = $validation->{$rules};
158182
}
159183

160-
return $this->validator->withRequest($this->request)->setRules($rules, $messages)->run();
184+
$this->validator->setRules($rules, $messages);
161185
}
162186
}

tests/system/ControllerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ public function testValidateWithStringRulesFoundUseMessagesParameter()
168168
$this->assertSame('You must choose a username.', Services::validation()->getError());
169169
}
170170

171+
public function testValidateData()
172+
{
173+
// make sure we can instantiate one
174+
$this->controller = new Controller();
175+
$this->controller->initController($this->request, $this->response, $this->logger);
176+
177+
$method = $this->getPrivateMethodInvoker($this->controller, 'validateData');
178+
179+
$data = [
180+
'username' => 'mike',
181+
'password' => '123',
182+
];
183+
$rule = [
184+
'username' => 'required',
185+
'password' => 'required|min_length[10]',
186+
];
187+
$this->assertFalse($method($data, $rule));
188+
$this->assertSame(
189+
'The password field must be at least 10 characters in length.',
190+
Services::validation()->getError('password')
191+
);
192+
}
193+
171194
public function testHelpers()
172195
{
173196
$this->controller = new class () extends Controller {

user_guide_src/source/incoming/controllers.rst

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,13 @@ inside the controller::
350350
protected $helpers = ['url', 'form'];
351351
}
352352

353+
.. _controllers-validating-data:
354+
353355
Validating data
354-
======================
356+
===============
357+
358+
$this->validate()
359+
-----------------
355360

356361
To simplify data checking, the controller also provides the convenience method ``validate()``.
357362
The method accepts an array of rules in the first parameter,
@@ -391,6 +396,31 @@ the ``$rules`` array with the name of the group as defined in ``Config\Validatio
391396

392397
.. 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.
393398

399+
$this->validateData()
400+
---------------------
401+
402+
Sometimes you may want to check the controller method parameters or other custom data.
403+
In that case, you can use the ``$this->validateData()`` method.
404+
The method accepts an array of data to validate in the first parameter::
405+
406+
public function product(int $id)
407+
{
408+
$data = [
409+
'id' => $id,
410+
'name' => $this->request->getVar('name'),
411+
];
412+
$rule = [
413+
'id' => 'integer',
414+
'name' => 'required|max_length[255]',
415+
];
416+
417+
if (! $this->validateData($data, $rule) {
418+
// ...
419+
}
420+
421+
// ...
422+
}
423+
394424
That's it!
395425
==========
396426

user_guide_src/source/libraries/validation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ If you submit the form you should simply see the form reload. That's
152152
because you haven't set up any validation rules in ``$this->validate()`` yet.
153153

154154
The ``validate()`` method is a method in the Controller. It uses
155-
the **Validation class** inside. See *Validating data* in :doc:`/incoming/controllers`.
155+
the **Validation class** inside. See :ref:`controllers-validating-data`.
156156

157157
.. note:: Since you haven't told the ``validate()`` method to validate anything
158158
yet, it **returns false** (boolean false) **by default**. The ``validate()``

0 commit comments

Comments
 (0)