Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve Validation Placeholder error message #8639

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,9 @@ protected function fillPlaceholders(array $rules, array $data): array
// Check if the validation rule for the placeholder exists
if ($placeholderRules === null) {
throw new LogicException(
'No validation rules for the placeholder: ' . $field
'No validation rules for the placeholder: "' . $field
. '". You must set the validation rules for the field.'
. ' See <https://codeigniter4.github.io/userguide/libraries/validation.html#validation-placeholders>.'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
. ' See <https://codeigniter4.github.io/userguide/libraries/validation.html#validation-placeholders>.'
. ' See <https://codeigniter.com/user_guide/libraries/validation.html#validation-placeholders>.'

Consistent link

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Config\Database;
use Config\Services;
use InvalidArgumentException;
use LogicException;
use Tests\Support\Validation\TestRules;

/**
Expand Down Expand Up @@ -139,6 +140,34 @@ public function testIsUniqueWithIgnoreValuePlaceholder(): void
$this->assertTrue($this->validation->run($data));
}

public function testIsUniqueWithPlaceholderAndNoValidationRulesForIt(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('No validation rules for the placeholder: "id". You must set the validation rules for the field.');

$this->hasInDatabase('user', [
'name' => 'Derek',
'email' => '[email protected]',
'country' => 'GB',
]);

$row = Database::connect()
->table('user')
->limit(1)
->get()
->getRow();

$data = [
'id' => $row->id,
'email' => '[email protected]',
];

$this->validation->setRules([
'email' => 'is_unique[user.email,id,{id}]',
]);
$this->validation->run($data);
}

public function testIsUniqueByManualRun(): void
{
Database::connect()
Expand Down
9 changes: 6 additions & 3 deletions user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,18 @@ Validation Placeholders
=======================

The Validation class provides a simple method to replace parts of your rules based on data that's being passed into it. This
sounds fairly obscure but can be especially handy with the ``is_unique`` validation rule. Placeholders are simply
sounds fairly obscure but can be especially handy with the ``is_unique`` validation rule.

Placeholders are simply
the name of the field (or array key) that was passed in as ``$data`` surrounded by curly brackets. It will be
replaced by the **value** of the matched incoming field. An example should clarify this:

.. literalinclude:: validation/020.php
:lines: 2-

.. note:: Since v4.3.5, you must set the validation rules for the placeholder
field (the ``id`` field in the sample code above) for security.
.. warning:: Since v4.3.5, you must set the validation rules for the placeholder
field (the ``id`` field in the sample code above) for security reasons. Because
attackers can send any data to your application.

In this set of rules, it states that the email address should be unique in the database, except for the row
that has an id matching the placeholder's value. Assuming that the form POST data had the following:
Expand Down
Loading