Skip to content

Commit 2ba179c

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.4
Conflicts: system/Validation/Validation.php
2 parents 02aaea1 + 0798239 commit 2ba179c

File tree

9 files changed

+124
-24
lines changed

9 files changed

+124
-24
lines changed

system/Test/FeatureTestTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ public function call(string $method, string $path, ?array $params = null)
167167
// Make sure filters are reset between tests
168168
Services::injectMock('filters', Services::filters(null, false));
169169

170+
// Make sure validation is reset between tests
171+
Services::injectMock('validation', Services::validation(null, false));
172+
170173
$response = $this->app
171174
->setContext('web')
172175
->setRequest($request)

system/Validation/Validation.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public function __construct($config, RendererInterface $view)
123123
* @param array|null $data The array of data to validate.
124124
* @param string|null $group The predefined group of rules to apply.
125125
* @param string|null $dbGroup The database group to use.
126+
*
127+
* @TODO Type ?string for $dbGroup should be removed.
128+
* See https://github.com/codeigniter4/CodeIgniter4/issues/6723
126129
*/
127130
public function run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool
128131
{
@@ -133,7 +136,7 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
133136
$this->data = $data;
134137
}
135138

136-
// i.e. is_unique
139+
// `DBGroup` is a reserved name. For is_unique and is_not_unique
137140
$data['DBGroup'] = $dbGroup;
138141

139142
$this->loadRuleSets();
@@ -206,18 +209,28 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
206209
}
207210

208211
/**
209-
* Runs the validation process, returning true or false
210-
* determining whether validation was successful or not.
212+
* Runs the validation process, returning true or false determining whether
213+
* validation was successful or not.
211214
*
212-
* @param array|bool|float|int|object|string|null $value The data to validate.
213-
* @param array|string $rule The validation rules.
214-
* @param string[] $errors The custom error message.
215+
* @param array|bool|float|int|object|string|null $value The data to validate.
216+
* @param array|string $rules The validation rules.
217+
* @param string[] $errors The custom error message.
218+
* @param string|null $dbGroup The database group to use.
215219
*/
216-
public function check($value, $rule, array $errors = []): bool
220+
public function check($value, $rules, array $errors = [], $dbGroup = null): bool
217221
{
218222
$this->reset();
219223

220-
return $this->setRule('check', null, $rule, $errors)->run(['check' => $value]);
224+
return $this->setRule(
225+
'check',
226+
null,
227+
$rules,
228+
$errors
229+
)->run(
230+
['check' => $value],
231+
null,
232+
$dbGroup
233+
);
221234
}
222235

223236
/**
@@ -711,6 +724,7 @@ protected function fillPlaceholders(array $rules, array $data): array
711724

712725
foreach ($placeholderFields as $field) {
713726
$validator ??= Services::validation(null, false);
727+
assert($validator instanceof Validation);
714728

715729
$placeholderRules = $rules[$field]['rules'] ?? null;
716730

@@ -731,7 +745,8 @@ protected function fillPlaceholders(array $rules, array $data): array
731745
}
732746

733747
// Validate the placeholder field
734-
if (! $validator->check($data[$field], implode('|', $placeholderRules))) {
748+
$dbGroup = $data['DBGroup'] ?? null;
749+
if (! $validator->check($data[$field], $placeholderRules, [], $dbGroup)) {
735750
// if fails, do nothing
736751
continue;
737752
}

system/Validation/ValidationInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
3232
* Check; runs the validation process, returning true or false
3333
* determining whether or not validation was successful.
3434
*
35-
* @param array|bool|float|int|object|string|null $value Value to validate.
35+
* @param array|bool|float|int|object|string|null $value Value to validate.
36+
* @param array|string $rules
3637
* @param string[] $errors
38+
* @param string|null $dbGroup The database group to use.
3739
*
3840
* @return bool True if valid, else false.
3941
*/
40-
public function check($value, string $rule, array $errors = []): bool;
42+
public function check($value, $rules, array $errors = [], $dbGroup = null): bool;
4143

4244
/**
4345
* Takes a Request object and grabs the input data to use from its

tests/system/Test/FeatureTestTraitTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,36 @@ public function testCallPostWithBody()
120120
$response->assertSee('Hello Mars!');
121121
}
122122

123+
public function testCallValidationTwice()
124+
{
125+
$this->withRoutes([
126+
[
127+
'post',
128+
'section/create',
129+
static function () {
130+
$validation = Services::validation();
131+
$validation->setRule('title', 'title', 'required|min_length[3]');
132+
133+
$post = Services::request()->getPost();
134+
135+
if ($validation->run($post)) {
136+
return 'Okay';
137+
}
138+
139+
return 'Invalid';
140+
},
141+
],
142+
]);
143+
144+
$response = $this->post('section/create', ['foo' => 'Mars']);
145+
146+
$response->assertSee('Invalid');
147+
148+
$response = $this->post('section/create', ['title' => 'Section Title']);
149+
150+
$response->assertSee('Okay');
151+
}
152+
123153
public function testCallPut()
124154
{
125155
$this->withRoutes([

tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Validation\Validation;
1717
use Config\Database;
1818
use Config\Services;
19+
use InvalidArgumentException;
1920
use Tests\Support\Validation\TestRules;
2021

2122
/**
@@ -82,7 +83,17 @@ public function testIsUniqueTrue(): void
8283
$this->assertTrue($this->validation->run($data));
8384
}
8485

85-
public function testIsUniqueIgnoresParams(): void
86+
public function testIsUniqueWithInvalidDBGroup(): void
87+
{
88+
$this->expectException(InvalidArgumentException::class);
89+
$this->expectExceptionMessage('invalidGroup is not a valid database connection group');
90+
91+
$this->validation->setRules(['email' => 'is_unique[user.email]']);
92+
$data = ['email' => '[email protected]'];
93+
$this->assertTrue($this->validation->run($data, null, 'invalidGroup'));
94+
}
95+
96+
public function testIsUniqueWithIgnoreValue(): void
8697
{
8798
$db = Database::connect();
8899
$db
@@ -102,7 +113,7 @@ public function testIsUniqueIgnoresParams(): void
102113
$this->assertTrue($this->validation->run($data));
103114
}
104115

105-
public function testIsUniqueIgnoresParamsPlaceholders(): void
116+
public function testIsUniqueWithIgnoreValuePlaceholder(): void
106117
{
107118
$this->hasInDatabase('user', [
108119
'name' => 'Derek',

user_guide_src/source/changelogs/v4.3.5.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ SECURITY
2121
Changes
2222
*******
2323

24-
- **make:cell** When creating a new cell, the controller would always have the ``Cell`` suffixed to the class name.
25-
For the view file, the final ``_cell`` is always removed.
26-
- **Cells** For compatibility with previous versions, view filenames ending with ``_cell`` can still be
27-
located by the ``Cell`` as long as auto-detection of view file is enabled (via setting the ``$view`` property
28-
to an empty string).
24+
- **make:cell command:** When creating a new cell, the controller would always have the ``Cell`` suffixed to the class name.
25+
For the view file, the final ``_cell`` is always removed.
26+
- **View Cells:** For compatibility with previous versions, view filenames ending with ``_cell`` can still be
27+
located by the ``Cell`` as long as auto-detection of view file is enabled (via setting the ``$view`` property
28+
to an empty string).
2929

3030
Deprecations
3131
************
@@ -37,8 +37,8 @@ Bugs Fixed
3737
**********
3838

3939
- **Validation:** Fixed a bug where a closure used in combination with ``permit_empty`` or ``if_exist`` rules was causing an error.
40-
- **make:cell** Fixed generating view files as classes.
41-
- **make:cell** Fixed treatment of single word class input for case-insensitive OS.
40+
- **make:cell command:** Fixed generating view files as classes.
41+
- **make:cell command:** Fixed treatment of single word class input for case-insensitive OS.
4242

4343
See the repo's
4444
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

user_guide_src/source/changelogs/v4.3.6.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ Release Date: Unreleased
1212
BREAKING
1313
********
1414

15+
Interface Changes
16+
=================
17+
18+
.. note:: As long as you have not extended the relevant CodeIgniter core classes
19+
or implemented these interfaces, all these changes are backward compatible
20+
and require no intervention.
21+
22+
ValidationInterface::check()
23+
----------------------------
24+
25+
- The second parameter has changed from ``string $rule`` to ``$rules``.
26+
- The optional fourth parameter ``$dbGroup = null`` has been added.
27+
28+
Method Signature Changes
29+
========================
30+
31+
Validation::check()
32+
-------------------
33+
34+
- The second parameter has changed from ``string $rule`` to ``$rules``.
35+
- The optional fourth parameter ``$dbGroup = null`` has been added.
36+
1537
Message Changes
1638
***************
1739

@@ -24,6 +46,11 @@ Deprecations
2446
Bugs Fixed
2547
**********
2648

49+
- **Validation:** Fixed a bug that ``$DBGroup`` is ignored when checking
50+
the value of a placeholder.
51+
- **Validation:** Fixed a bug that ``check()`` cannot specify non-default
52+
database group.
53+
2754
See the repo's
2855
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
2956
for a complete list of bugs fixed.

user_guide_src/source/installation/upgrade_436.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Breaking Changes
2121
Breaking Enhancements
2222
*********************
2323

24+
- The method signatures of ``ValidationInterface::check()`` and ``Validation::check()``
25+
have been changed. If you implement or extend them, update the signatures.
26+
2427
Project Files
2528
*************
2629

user_guide_src/source/libraries/validation.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -753,14 +753,23 @@ Your new custom rule could now be used just like any other rule:
753753
Allowing Parameters
754754
-------------------
755755

756-
If your method needs to work with parameters, the function will need a minimum of three parameters: the value to validate,
757-
the parameter string, and an array with all of the data that was submitted the form. The ``$data`` array is especially handy
756+
If your method needs to work with parameters, the function will need a minimum of three parameters:
757+
758+
1. the value to validate (``$value``)
759+
2. the parameter string (``$params``)
760+
3. an array with all of the data that was submitted the form (``$data``)
761+
4. (optional) a custom error string (``&$error``), just as described above.
762+
763+
.. warning:: The field values in ``$data`` are unvalidated (or may be invalid).
764+
Using unvalidated input data is a source of vulnerability. You must
765+
perform the necessary validation within your custom rules before using the
766+
data in ``$data``.
767+
768+
The ``$data`` array is especially handy
758769
for rules like ``required_with`` that needs to check the value of another submitted field to base its result on:
759770

760771
.. literalinclude:: validation/037.php
761772

762-
Custom errors can be returned as the fourth parameter ``&$error``, just as described above.
763-
764773
.. _validation-using-closure-rule:
765774

766775
Using Closure Rule

0 commit comments

Comments
 (0)