Skip to content

Commit 846466e

Browse files
authored
Merge pull request #8499 from kenjis/feat-Validation-run-DB-connection
feat: Validation::run() accepts DB connection
2 parents d0f9d20 + a33724c commit 846466e

File tree

6 files changed

+37
-13
lines changed

6 files changed

+37
-13
lines changed

deptrac.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ parameters:
233233
- I18n
234234
Validation:
235235
- HTTP
236+
- Database
236237
View:
237238
- Cache
238239
skip_violations:

system/Validation/Validation.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Validation;
1515

1616
use Closure;
17+
use CodeIgniter\Database\BaseConnection;
1718
use CodeIgniter\HTTP\Exceptions\HTTPException;
1819
use CodeIgniter\HTTP\IncomingRequest;
1920
use CodeIgniter\HTTP\Method;
@@ -128,14 +129,11 @@ public function __construct($config, RendererInterface $view)
128129
* Runs the validation process, returning true/false determining whether
129130
* validation was successful or not.
130131
*
131-
* @param array|null $data The array of data to validate.
132-
* @param string|null $group The predefined group of rules to apply.
133-
* @param string|null $dbGroup The database group to use.
134-
*
135-
* @TODO Type ?string for $dbGroup should be removed.
136-
* See https://github.com/codeigniter4/CodeIgniter4/issues/6723
132+
* @param array|null $data The array of data to validate.
133+
* @param string|null $group The predefined group of rules to apply.
134+
* @param array|BaseConnection|non-empty-string|null $dbGroup The database group to use.
137135
*/
138-
public function run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool
136+
public function run(?array $data = null, ?string $group = null, $dbGroup = null): bool
139137
{
140138
if ($data === null) {
141139
$data = $this->data;

system/Validation/ValidationInterface.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Validation;
1515

16+
use CodeIgniter\Database\BaseConnection;
1617
use CodeIgniter\HTTP\RequestInterface;
1718

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

3334
/**
3435
* Check; runs the validation process, returning true or false

tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ public function testIsUniqueTrue(): void
8585
$this->assertTrue($this->validation->run($data));
8686
}
8787

88+
public function testIsUniqueWithDBConnection(): void
89+
{
90+
$db = db_connect();
91+
$this->validation->setRules(['email' => 'is_unique[user.email]']);
92+
93+
$data = ['email' => '[email protected]'];
94+
$result = $this->validation->run($data, null, $db);
95+
96+
$this->assertTrue($result);
97+
}
98+
8899
public function testIsUniqueWithInvalidDBGroup(): void
89100
{
90101
$this->expectException(InvalidArgumentException::class);

user_guide_src/source/changelogs/v4.5.0.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Interface Changes
8787
the ``ResponseInterface::setCookie()`` has been fixed from ``''`` to ``0``.
8888
- **Logger:** The `psr/log <https://packagist.org/packages/psr/log>`_ package has
8989
been upgraded to v3.0.0.
90+
- **Validation:** The method signature of ``ValidationInterface::run()`` has been
91+
changed. The ``?string`` typehint on the ``$dbGroup`` parameter was removed.
9092

9193
.. _v450-method-signature-changes:
9294

@@ -136,6 +138,8 @@ Others
136138
that implements the PSR-3 interface have been fixed. The ``bool`` return
137139
types are changed to ``void``. The ``$message`` parameters now have
138140
``string|Stringable`` types.
141+
- **Validation:** The method signature of ``Validation::run()`` has been
142+
changed. The ``?string`` typehint on the ``$dbGroup`` parameter was removed.
139143

140144
.. _v450-removed-deprecated-items:
141145

@@ -316,8 +320,12 @@ See :ref:`Using CodeIgniter’s Model <model-update-only-changed>` for details.
316320
Libraries
317321
=========
318322

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

322330
Helpers and Functions
323331
=====================

user_guide_src/source/installation/upgrade_450.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ using them, upgrade your code. See :ref:`ChangeLog <v450-removed-deprecated-item
213213
Breaking Enhancements
214214
*********************
215215

216+
- The method signatures of ``Validation::run()`` and ``ValidationInterface::run()``
217+
have been changed. The ``?string`` typehint on the ``$dbGroup`` parameter was
218+
removed. Extending classes should likewise remove the parameter so as not to
219+
break LSP.
220+
216221
Project Files
217222
*************
218223

0 commit comments

Comments
 (0)