diff --git a/deptrac.yaml b/deptrac.yaml index 4d6cb8912820..4ae859916b9a 100644 --- a/deptrac.yaml +++ b/deptrac.yaml @@ -219,6 +219,7 @@ parameters: - I18n Validation: - HTTP + - Database View: - Cache skip_violations: diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 306d105c3560..ef3056414c3b 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -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; @@ -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; diff --git a/system/Validation/ValidationInterface.php b/system/Validation/ValidationInterface.php index a4ed551b9e62..d2881f49549c 100644 --- a/system/Validation/ValidationInterface.php +++ b/system/Validation/ValidationInterface.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Validation; +use CodeIgniter\Database\BaseConnection; use CodeIgniter\HTTP\RequestInterface; /** @@ -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 diff --git a/tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php b/tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php index e33ba58b9564..08a45574f32d 100644 --- a/tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php +++ b/tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php @@ -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); diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index d019ca469606..4bfefe60848b 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -80,6 +80,8 @@ Interface Changes the ``ResponseInterface::setCookie()`` has been fixed from ``''`` to ``0``. - **Logger:** The `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: @@ -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: @@ -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 ===================== diff --git a/user_guide_src/source/installation/upgrade_450.rst b/user_guide_src/source/installation/upgrade_450.rst index 92eb37b80d57..62b4306cf158 100644 --- a/user_guide_src/source/installation/upgrade_450.rst +++ b/user_guide_src/source/installation/upgrade_450.rst @@ -199,6 +199,11 @@ using them, upgrade your code. See :ref:`ChangeLog