Skip to content

Commit 5ebe2b7

Browse files
committed
refactor: improve Validation error message for placeholder
1 parent fd6d674 commit 5ebe2b7

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

system/Validation/Validation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,9 @@ protected function fillPlaceholders(array $rules, array $data): array
790790
// Check if the validation rule for the placeholder exists
791791
if ($placeholderRules === null) {
792792
throw new LogicException(
793-
'No validation rules for the placeholder: ' . $field
793+
'No validation rules for the placeholder: "' . $field
794+
. '". You must set the validation rules for the field.'
795+
. ' See <https://codeigniter4.github.io/userguide/libraries/validation.html#validation-placeholders>.'
794796
);
795797
}
796798

tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Config\Database;
1818
use Config\Services;
1919
use InvalidArgumentException;
20+
use LogicException;
2021
use Tests\Support\Validation\TestRules;
2122

2223
/**
@@ -139,6 +140,34 @@ public function testIsUniqueWithIgnoreValuePlaceholder(): void
139140
$this->assertTrue($this->validation->run($data));
140141
}
141142

143+
public function testIsUniqueWithPlaceholderAndNoValidationRulesForIt(): void
144+
{
145+
$this->expectException(LogicException::class);
146+
$this->expectExceptionMessage('No validation rules for the placeholder: "id". You must set the validation rules for the field.');
147+
148+
$this->hasInDatabase('user', [
149+
'name' => 'Derek',
150+
'email' => 'derek@world.co.uk',
151+
'country' => 'GB',
152+
]);
153+
154+
$row = Database::connect()
155+
->table('user')
156+
->limit(1)
157+
->get()
158+
->getRow();
159+
160+
$data = [
161+
'id' => $row->id,
162+
'email' => 'derek@world.co.uk',
163+
];
164+
165+
$this->validation->setRules([
166+
'email' => 'is_unique[user.email,id,{id}]',
167+
]);
168+
$this->validation->run($data);
169+
}
170+
142171
public function testIsUniqueByManualRun(): void
143172
{
144173
Database::connect()

0 commit comments

Comments
 (0)