Skip to content

Commit ba7d36c

Browse files
authored
Merge pull request #6241 from kenjis/fix-validation-setRule-errors
fix: custom validation error is cleared when calling setRule() twice
2 parents c71a0a6 + 8baf85f commit ba7d36c

3 files changed

Lines changed: 73 additions & 7 deletions

File tree

system/Validation/Validation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public function setRule(string $field, ?string $label, $rules, array $errors = [
399399
$ruleSet[$field]['errors'] = $errors;
400400
}
401401

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

404404
return $this;
405405
}

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: 36 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);
322+
323+
$this->assertSame('Nope. Not a number.', $this->validation->getError('foo'));
324+
$this->assertSame('No. Not a number.', $this->validation->getError('bar'));
325+
}
315326

316-
$this->validation->setRules(['foo' => 'is_numeric'], $messages);
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

0 commit comments

Comments
 (0)