Skip to content

Commit 9e5b146

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.3
2 parents da5470e + ddccfc0 commit 9e5b146

File tree

6 files changed

+104
-10
lines changed

6 files changed

+104
-10
lines changed

system/Pager/Pager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ protected function displayLinks(string $group, string $template): string
122122

123123
$pager = new PagerRenderer($this->getDetails($group));
124124

125-
return $this->view->setVar('pager', $pager)->render($this->config->templates[$template]);
125+
return $this->view->setVar('pager', $pager)
126+
->render($this->config->templates[$template], null, false);
126127
}
127128

128129
/**

system/Validation/Validation.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
147147

148148
if (strpos($field, '*') !== false) {
149149
$values = array_filter(array_flatten_with_dots($data), static fn ($key) => preg_match(
150-
'/^' . str_replace('\.\*', '\..+', preg_quote($field, '/')) . '$/',
150+
'/^'
151+
. str_replace(['\.\*', '\*\.'], ['\..+', '.+\.'], preg_quote($field, '/'))
152+
. '$/',
151153
$key
152154
), ARRAY_FILTER_USE_KEY);
153155
// if keys not found
@@ -399,7 +401,7 @@ public function setRule(string $field, ?string $label, $rules, array $errors = [
399401
$ruleSet[$field]['errors'] = $errors;
400402
}
401403

402-
$this->setRules($ruleSet + $this->getRules());
404+
$this->setRules($ruleSet + $this->getRules(), $this->customErrors);
403405

404406
return $this;
405407
}
@@ -657,7 +659,7 @@ public function getError(?string $field = null): string
657659
}
658660

659661
$errors = array_filter($this->getErrors(), static fn ($key) => preg_match(
660-
'/^' . str_replace('\.\*', '\..+', preg_quote($field, '/')) . '$/',
662+
'/^' . str_replace(['\.\*', '\*\.'], ['\..+', '.+\.'], preg_quote($field, '/')) . '$/',
661663
$key
662664
), ARRAY_FILTER_USE_KEY);
663665

tests/system/Validation/StrictRules/ValidationTest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,50 @@ public function testRunReturnsLocalizedErrors(): void
202202

203203
public function testRunWithCustomErrors(): void
204204
{
205-
$data = ['foo' => 'notanumber'];
206-
205+
$data = [
206+
'foo' => 'notanumber',
207+
'bar' => 'notanumber',
208+
];
207209
$messages = [
208210
'foo' => [
209211
'is_numeric' => 'Nope. Not a number.',
210212
],
213+
'bar' => [
214+
'is_numeric' => 'No. Not a number.',
215+
],
211216
];
217+
$this->validation->setRules(['foo' => 'is_numeric', 'bar' => 'is_numeric'], $messages);
218+
$this->validation->run($data);
219+
220+
$this->assertSame('Nope. Not a number.', $this->validation->getError('foo'));
221+
$this->assertSame('No. Not a number.', $this->validation->getError('bar'));
222+
}
212223

213-
$this->validation->setRules(['foo' => 'is_numeric'], $messages);
224+
/**
225+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6239
226+
*/
227+
public function testSetRuleWithCustomErrors(): void
228+
{
229+
$data = [
230+
'foo' => 'notanumber',
231+
'bar' => 'notanumber',
232+
];
233+
$this->validation->setRule(
234+
'foo',
235+
'Foo',
236+
['foo' => 'is_numeric'],
237+
['is_numeric' => 'Nope. Not a number.']
238+
);
239+
$this->validation->setRule(
240+
'bar',
241+
'Bar',
242+
['bar' => 'is_numeric'],
243+
['is_numeric' => 'Nope. Not a number.']
244+
);
214245
$this->validation->run($data);
246+
215247
$this->assertSame('Nope. Not a number.', $this->validation->getError('foo'));
248+
$this->assertSame('Nope. Not a number.', $this->validation->getError('bar'));
216249
}
217250

218251
public function testCheck(): void

tests/system/Validation/ValidationTest.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,50 @@ public function testRunReturnsLocalizedErrors(): void
305305

306306
public function testRunWithCustomErrors(): void
307307
{
308-
$data = ['foo' => 'notanumber'];
309-
308+
$data = [
309+
'foo' => 'notanumber',
310+
'bar' => 'notanumber',
311+
];
310312
$messages = [
311313
'foo' => [
312314
'is_numeric' => 'Nope. Not a number.',
313315
],
316+
'bar' => [
317+
'is_numeric' => 'No. Not a number.',
318+
],
314319
];
320+
$this->validation->setRules(['foo' => 'is_numeric', 'bar' => 'is_numeric'], $messages);
321+
$this->validation->run($data);
315322

316-
$this->validation->setRules(['foo' => 'is_numeric'], $messages);
323+
$this->assertSame('Nope. Not a number.', $this->validation->getError('foo'));
324+
$this->assertSame('No. Not a number.', $this->validation->getError('bar'));
325+
}
326+
327+
/**
328+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6239
329+
*/
330+
public function testSetRuleWithCustomErrors(): void
331+
{
332+
$data = [
333+
'foo' => 'notanumber',
334+
'bar' => 'notanumber',
335+
];
336+
$this->validation->setRule(
337+
'foo',
338+
'Foo',
339+
['foo' => 'is_numeric'],
340+
['is_numeric' => 'Nope. Not a number.']
341+
);
342+
$this->validation->setRule(
343+
'bar',
344+
'Bar',
345+
['bar' => 'is_numeric'],
346+
['is_numeric' => 'Nope. Not a number.']
347+
);
317348
$this->validation->run($data);
349+
318350
$this->assertSame('Nope. Not a number.', $this->validation->getError('foo'));
351+
$this->assertSame('Nope. Not a number.', $this->validation->getError('bar'));
319352
}
320353

321354
public function testCheck(): void
@@ -1098,6 +1131,12 @@ public function validationArrayDataCaseProvider(): iterable
10981131
['foo' => ['boz']],
10991132
]],
11001133
];
1134+
1135+
yield 'leading-asterisk' => [
1136+
true,
1137+
['*.foo' => 'required'],
1138+
[['foo' => 'bar']],
1139+
];
11011140
}
11021141

11031142
/**
@@ -1324,4 +1363,17 @@ public function testNestedArrayThrowsException(): void
13241363
'beneficiaries_accounts.account_2.purpose' => 'The PURPOSE field must be at least 3 characters in length.',
13251364
], $this->validation->getErrors());
13261365
}
1366+
1367+
public function testRuleWithLeadingAsterisk(): void
1368+
{
1369+
$data = [
1370+
['foo' => 1],
1371+
['foo' => null],
1372+
];
1373+
1374+
$this->validation->setRules(['*.foo' => 'required'], ['1.foo' => ['required' => 'Required {field}']]);
1375+
1376+
$this->assertFalse($this->validation->run($data));
1377+
$this->assertSame('Required *.foo', $this->validation->getError('*.foo'));
1378+
}
13271379
}

user_guide_src/source/changelogs/v4.2.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Changes
2525
*******
2626

2727
- Fixed: ``BaseBuilder::increment()`` and ``BaseBuilder::decrement()`` do not reset the ``BaseBuilder`` state after a query.
28+
- Fixed: Validation of fields with a leading asterisk (wildcard).
2829
- Now ``CLIRequest::isCLI()`` always returns true.
2930
- Now ``IncommingRequest::isCLI()`` always returns false.
3031

user_guide_src/source/incoming/routing.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ a valid class/method pair, just like you would show in any route, or a Closure:
526526

527527
.. literalinclude:: routing/051.php
528528

529+
.. note:: The ``set404Override()`` method does not change the Response status code to ``404``.
530+
If you don't set the status code in the controller you set, the default status code ``200``
531+
will be returned. See :php:func:`Response::setStatusCode() <setStatusCode>` for
532+
information on how to set the status code.
533+
529534
Route processing by priority
530535
============================
531536

0 commit comments

Comments
 (0)