Skip to content

Commit 36c0de9

Browse files
committed
fix: traditional validation rule matches and differs
Match CI3 behaviros.
1 parent e89fbd8 commit 36c0de9

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

system/Validation/Rules.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ class Rules
2424
/**
2525
* The value does not match another field in $data.
2626
*
27-
* @param array $data Other field/value pairs
27+
* @param string|null $str
28+
* @param array $data Other field/value pairs
2829
*/
29-
public function differs(?string $str, string $field, array $data): bool
30+
public function differs($str, string $field, array $data): bool
3031
{
3132
if (strpos($field, '.') !== false) {
3233
return $str !== dot_array_search($field, $data);
@@ -177,15 +178,16 @@ public function less_than_equal_to(?string $str, string $max): bool
177178
/**
178179
* Matches the value of another field in $data.
179180
*
180-
* @param array $data Other field/value pairs
181+
* @param string|null $str
182+
* @param array $data Other field/value pairs
181183
*/
182-
public function matches(?string $str, string $field, array $data): bool
184+
public function matches($str, string $field, array $data): bool
183185
{
184186
if (strpos($field, '.') !== false) {
185187
return $str === dot_array_search($field, $data);
186188
}
187189

188-
return array_key_exists($field, $data) && $str === $data[$field];
190+
return isset($data[$field]) ? ($str === $data[$field]) : false;
189191
}
190192

191193
/**

tests/system/Validation/RulesTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,13 @@ public static function provideMatches(): iterable
303303
yield from [
304304
'foo bar not exist' => [[], false],
305305
'bar not exist' => [['foo' => null], false],
306-
'foo not exist' => [['bar' => null], true], // Strict Rule: false
307-
'foo bar null' => [['foo' => null, 'bar' => null], true],
306+
'foo not exist' => [['bar' => null], false],
307+
'foo bar null' => [['foo' => null, 'bar' => null], false], // Strict Rule: true
308308
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], true],
309309
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], false],
310-
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false], // Strict Rule: true
310+
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true],
311311
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
312-
'foo bar bool match' => [['foo' => true, 'bar' => true], false], // Strict Rule: true
312+
'foo bar bool match' => [['foo' => true, 'bar' => true], true],
313313
];
314314
}
315315

@@ -348,9 +348,9 @@ public static function provideDiffers(): iterable
348348
'foo bar null' => [['foo' => null, 'bar' => null], false],
349349
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], false],
350350
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], true],
351-
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true], // Strict Rule: false
351+
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false],
352352
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], true],
353-
'foo bar bool match' => [['foo' => true, 'bar' => true], true], // Strict Rule: false
353+
'foo bar bool match' => [['foo' => true, 'bar' => true], false],
354354
];
355355
}
356356

0 commit comments

Comments
 (0)