Skip to content

Commit 0dbd648

Browse files
committed
refector: rewrite required_without[] logic
1 parent 7ecd9b3 commit 0dbd648

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

system/Validation/Rules.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function required_with($str = null, ?string $fields = null, array $data =
280280
*
281281
* @param string|null $str
282282
*/
283-
public function required_without($str = null, ?string $fields = null, array $data = [], string $keyField = null): bool
283+
public function required_without($str = null, ?string $fields = null, array $data = [], ?string $keyField = null): bool
284284
{
285285
if ($fields === null || empty($data) || $keyField === null) {
286286
throw new InvalidArgumentException('You must supply the parameters: fields, data, keyField.');
@@ -296,11 +296,26 @@ public function required_without($str = null, ?string $fields = null, array $dat
296296
return true;
297297
}
298298

299+
if (strpos($keyField, '.') !== false) {
300+
$keyFieldArray = explode('.', $keyField);
301+
$nowKeyField = $keyFieldArray[1];
302+
}
303+
299304
// Still here? Then we fail this test if
300305
// any of the fields are not present in $data
301306
foreach ($fields as $field) {
302-
if ((strpos($field, '.') === false && (! array_key_exists($field, $data) || empty($data[$field]))) || (strpos($field, '.') !== false && empty(dot_array_search($field, $data)))) {
303-
return false;
307+
if ((strpos($field, '.') === false) && (! array_key_exists($field, $data))) {
308+
return ! empty($data[$field]);
309+
}
310+
if (strpos($field, '.') !== false) {
311+
$fieldData = dot_array_search($field, $data);
312+
if (is_array($fieldData)) {
313+
return ! empty(dot_array_search($field, $data)[$nowKeyField]);
314+
}
315+
$nowField = str_replace('*', $nowKeyField, $field);
316+
$nowFieldVaule = dot_array_search($nowField, $data);
317+
318+
return (bool) (null !== $nowFieldVaule);
304319
}
305320
}
306321

0 commit comments

Comments
 (0)