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

feat: Validation::run() accepts DB connection #8499

Merged
merged 3 commits into from
Feb 7, 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
1 change: 1 addition & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ parameters:
- I18n
Validation:
- HTTP
- Database
View:
- Cache
skip_violations:
Expand Down
12 changes: 5 additions & 7 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\Validation;

use Closure;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
Expand Down Expand Up @@ -128,14 +129,11 @@ public function __construct($config, RendererInterface $view)
* Runs the validation process, returning true/false determining whether
* validation was successful or not.
*
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param string|null $dbGroup The database group to use.
*
* @TODO Type ?string for $dbGroup should be removed.
* See https://github.com/codeigniter4/CodeIgniter4/issues/6723
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param array|BaseConnection|non-empty-string|null $dbGroup The database group to use.
*/
public function run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool
public function run(?array $data = null, ?string $group = null, $dbGroup = null): bool
{
if ($data === null) {
$data = $this->data;
Expand Down
9 changes: 5 additions & 4 deletions system/Validation/ValidationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Validation;

use CodeIgniter\Database\BaseConnection;
use CodeIgniter\HTTP\RequestInterface;

/**
Expand All @@ -24,11 +25,11 @@ interface ValidationInterface
* Runs the validation process, returning true/false determining whether
* validation was successful or not.
*
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param string|null $dbGroup The database group to use.
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param array|BaseConnection|non-empty-string|null $dbGroup The database group to use.
*/
public function run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool;
public function run(?array $data = null, ?string $group = null, $dbGroup = null): bool;

/**
* Check; runs the validation process, returning true or false
Expand Down
11 changes: 11 additions & 0 deletions tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public function testIsUniqueTrue(): void
$this->assertTrue($this->validation->run($data));
}

public function testIsUniqueWithDBConnection(): void
{
$db = db_connect();
$this->validation->setRules(['email' => 'is_unique[user.email]']);

$data = ['email' => 'derek@world.co.uk'];
$result = $this->validation->run($data, null, $db);

$this->assertTrue($result);
}

public function testIsUniqueWithInvalidDBGroup(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
12 changes: 10 additions & 2 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Interface Changes
the ``ResponseInterface::setCookie()`` has been fixed from ``''`` to ``0``.
- **Logger:** The `psr/log <https://packagist.org/packages/psr/log>`_ package has
been upgraded to v3.0.0.
- **Validation:** The method signature of ``ValidationInterface::run()`` has been
changed. The ``?string`` typehint on the ``$dbGroup`` parameter was removed.

.. _v450-method-signature-changes:

Expand Down Expand Up @@ -129,6 +131,8 @@ Others
that implements the PSR-3 interface have been fixed. The ``bool`` return
types are changed to ``void``. The ``$message`` parameters now have
``string|Stringable`` types.
- **Validation:** The method signature of ``Validation::run()`` has been
changed. The ``?string`` typehint on the ``$dbGroup`` parameter was removed.

.. _v450-removed-deprecated-items:

Expand Down Expand Up @@ -294,8 +298,12 @@ findAll(0) Behavior
Libraries
=========

- **Validation:** Added the new rule ``field_exists`` that checks the filed
exists in the data to be validated.
- **Validation:**
- Added the new rule ``field_exists`` that checks the filed exists in the
data to be validated.
- The ``$dbGroup`` parameter of ``Validation::run()`` now accepts not only
a database group name, but also a database connection instance or an array
of database settings.

Helpers and Functions
=====================
Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/installation/upgrade_450.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ using them, upgrade your code. See :ref:`ChangeLog <v450-removed-deprecated-item
Breaking Enhancements
*********************

- The method signatures of ``Validation::run()`` and ``ValidationInterface::run()``
have been changed. The ``?string`` typehint on the ``$dbGroup`` parameter was
removed. Extending classes should likewise remove the parameter so as not to
break LSP.

Project Files
*************

Expand Down
Loading