mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Merge pull request #8499 from kenjis/feat-Validation-run-DB-connection
feat: Validation::run() accepts DB connection
This commit is contained in:
commit
846466e82a
@ -233,6 +233,7 @@ parameters:
|
||||
- I18n
|
||||
Validation:
|
||||
- HTTP
|
||||
- Database
|
||||
View:
|
||||
- Cache
|
||||
skip_violations:
|
||||
|
@ -14,6 +14,7 @@ declare(strict_types=1);
|
||||
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 @@ class Validation implements 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.
|
||||
*
|
||||
* @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;
|
||||
|
@ -13,6 +13,7 @@ declare(strict_types=1);
|
||||
|
||||
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
|
||||
|
@ -85,6 +85,17 @@ class DatabaseRelatedRulesTest extends CIUnitTestCase
|
||||
$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);
|
||||
|
@ -87,6 +87,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:
|
||||
|
||||
@ -136,6 +138,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:
|
||||
|
||||
@ -316,8 +320,12 @@ See :ref:`Using CodeIgniter’s Model <model-update-only-changed>` for details.
|
||||
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
|
||||
=====================
|
||||
|
@ -213,6 +213,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
|
||||
*************
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user