Skip to content

Commit

Permalink
WIP 2 (for multiselect)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Nov 7, 2022
1 parent 58bf349 commit f7ca451
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/InArrayValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,49 @@ public function isValid($value)
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();

if (is_array($value)) {
$notInArrayValues = [];
foreach ($value as $val) {
if (! $this->inArray($val)) {
$notInArrayValues[] = $val;
}
}

if (empty($notInArrayValues)) {
return true;
}

$value = implode(', ', $notInArrayValues);
if (count($notInArrayValues) > 1) {
$this->addMessage(sprintf(
$this->translate("'%s' were not found in the haystack"), //BETTER CODE REQUIRED HERE
$value
));

return false;
}

$this->addMessage(sprintf(
$this->translate("'%s' was not found in the haystack"),
$value
));

return false;

} elseif ($this->inArray($value)) {
return true;
}

$this->addMessage(sprintf(
$this->translate("'%s' was not found in the haystack"),
$value
));

return false;
}

private function inArray($value)
{
if ($this->getRecursive()) {
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($this->getHaystack()));
foreach ($iterator as $element) {
Expand All @@ -140,11 +183,6 @@ public function isValid($value)
return true;
}

$this->addMessage(sprintf(
$this->translate("'%s' was not found in the haystack"),
$value
));

return false;
}
}

0 comments on commit f7ca451

Please sign in to comment.