Skip to content

Commit d7099cc

Browse files
committed
fix: TypeError in Rules::is_unique()
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
1 parent 5ab8487 commit d7099cc

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

system/Validation/Rules.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Validation;
1313

14+
use CodeIgniter\Validation\StrictRules\Rules as StrictRules;
1415
use Config\Database;
1516
use InvalidArgumentException;
1617

@@ -19,6 +20,15 @@
1920
*/
2021
class Rules
2122
{
23+
private ?StrictRules $strictRules = null;
24+
25+
private function createStrictRules(): void
26+
{
27+
if ($this->strictRules === null) {
28+
$this->strictRules = new StrictRules();
29+
}
30+
}
31+
2232
/**
2333
* The value does not match another field in $data.
2434
*
@@ -125,21 +135,9 @@ public function in_list(?string $value, string $list): bool
125135
*/
126136
public function is_unique(?string $str, string $field, array $data): bool
127137
{
128-
[$field, $ignoreField, $ignoreValue] = array_pad(explode(',', $field), 3, null);
129-
130-
sscanf($field, '%[^.].%[^.]', $table, $field);
131-
132-
$row = Database::connect($data['DBGroup'] ?? null)
133-
->table($table)
134-
->select('1')
135-
->where($field, $str)
136-
->limit(1);
137-
138-
if (! empty($ignoreField) && ! empty($ignoreValue) && ! preg_match('/^\{(\w+)\}$/', $ignoreValue)) {
139-
$row = $row->where("{$ignoreField} !=", $ignoreValue);
140-
}
138+
$this->createStrictRules();
141139

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

145143
/**

system/Validation/StrictRules/Rules.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,25 @@ public function in_list($value, string $list): bool
151151
*/
152152
public function is_unique($str, string $field, array $data): bool
153153
{
154-
return $this->nonStrictRules->is_unique($str, $field, $data);
154+
[$field, $ignoreField, $ignoreValue] = array_pad(
155+
explode(',', $field),
156+
3,
157+
null
158+
);
159+
160+
sscanf($field, '%[^.].%[^.]', $table, $field);
161+
162+
$row = Database::connect($data['DBGroup'] ?? null)
163+
->table($table)
164+
->select('1')
165+
->where($field, $str)
166+
->limit(1);
167+
168+
if (! empty($ignoreField) && ! empty($ignoreValue) && ! preg_match('/^\{(\w+)\}$/', $ignoreValue)) {
169+
$row = $row->where("{$ignoreField} !=", $ignoreValue);
170+
}
171+
172+
return $row->get()->getRow() === null;
155173
}
156174

157175
/**

0 commit comments

Comments
 (0)