Skip to content

Commit

Permalink
fix: TypeError in Rules::is_unique()
Browse files Browse the repository at this point in the history
TypeError : CodeIgniter\Validation\Rules::is_unique(): Argument #1 ($str) must be of type ?string, int given, called in /.../CodeIgniter4/system/Validation/StrictRules/Rules.php on line 154
 /.../CodeIgniter4/system/Validation/Rules.php:126
  • Loading branch information
kenjis committed Jan 11, 2023
1 parent 5ab8487 commit d7099cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
26 changes: 12 additions & 14 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Validation;

use CodeIgniter\Validation\StrictRules\Rules as StrictRules;
use Config\Database;
use InvalidArgumentException;

Expand All @@ -19,6 +20,15 @@
*/
class Rules
{
private ?StrictRules $strictRules = null;

private function createStrictRules(): void
{
if ($this->strictRules === null) {
$this->strictRules = new StrictRules();
}
}

/**
* The value does not match another field in $data.
*
Expand Down Expand Up @@ -125,21 +135,9 @@ public function in_list(?string $value, string $list): bool
*/
public function is_unique(?string $str, string $field, array $data): bool
{
[$field, $ignoreField, $ignoreValue] = array_pad(explode(',', $field), 3, null);

sscanf($field, '%[^.].%[^.]', $table, $field);

$row = Database::connect($data['DBGroup'] ?? null)
->table($table)
->select('1')
->where($field, $str)
->limit(1);

if (! empty($ignoreField) && ! empty($ignoreValue) && ! preg_match('/^\{(\w+)\}$/', $ignoreValue)) {
$row = $row->where("{$ignoreField} !=", $ignoreValue);
}
$this->createStrictRules();

return $row->get()->getRow() === null;
return $this->strictRules->is_unique($str, $field, $data);
}

/**
Expand Down
20 changes: 19 additions & 1 deletion system/Validation/StrictRules/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,25 @@ public function in_list($value, string $list): bool
*/
public function is_unique($str, string $field, array $data): bool
{
return $this->nonStrictRules->is_unique($str, $field, $data);
[$field, $ignoreField, $ignoreValue] = array_pad(
explode(',', $field),
3,
null
);

sscanf($field, '%[^.].%[^.]', $table, $field);

$row = Database::connect($data['DBGroup'] ?? null)
->table($table)
->select('1')
->where($field, $str)
->limit(1);

if (! empty($ignoreField) && ! empty($ignoreValue) && ! preg_match('/^\{(\w+)\}$/', $ignoreValue)) {
$row = $row->where("{$ignoreField} !=", $ignoreValue);
}

return $row->get()->getRow() === null;
}

/**
Expand Down

0 comments on commit d7099cc

Please sign in to comment.