Skip to content

Commit

Permalink
refector: rewrite required_without[] logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ping-yee committed Sep 23, 2022
1 parent 3ab0ce7 commit 5cb0c8d
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function required_with($str = null, ?string $fields = null, array $data =
*
* @param string|null $str
*/
public function required_without($str = null, ?string $fields = null, array $data = [], string $keyField = null): bool
public function required_without($str = null, ?string $fields = null, array $data = [], ?string $keyField = null): bool
{
if ($fields === null || empty($data) || $keyField === null) {
throw new InvalidArgumentException('You must supply the parameters: fields, data, keyField.');
Expand All @@ -296,11 +296,26 @@ public function required_without($str = null, ?string $fields = null, array $dat
return true;
}

if (strpos($keyField, '.') !== false) {
$keyFieldArray = explode('.', $keyField);
$nowKeyField = $keyFieldArray[1];
}

// Still here? Then we fail this test if
// any of the fields are not present in $data
foreach ($fields as $field) {
if ((strpos($field, '.') === false && (! array_key_exists($field, $data) || empty($data[$field]))) || (strpos($field, '.') !== false && empty(dot_array_search($field, $data)))) {
return false;
if ((strpos($field, '.') === false) && (! array_key_exists($field, $data))) {
return ! empty($data[$field]);
}
if (strpos($field, '.') !== false) {
$fieldData = dot_array_search($field, $data);
if (is_array($fieldData)) {
return ! empty(dot_array_search($field, $data)[$nowKeyField]);
}
$nowField = str_replace('*', $nowKeyField, $field);
$nowFieldVaule = dot_array_search($nowField, $data);

return (bool) (null !== $nowFieldVaule);
}
}

Expand Down

0 comments on commit 5cb0c8d

Please sign in to comment.