Skip to content

Commit 615b2ba

Browse files
committed
Move AJAX check towards RequiredValidator class
1 parent a173c86 commit 615b2ba

3 files changed

Lines changed: 13 additions & 21 deletions

File tree

Validator/RequiredValidator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
namespace Loki\Components\Validator;
44

55
use Loki\Components\Component\ComponentInterface;
6+
use Loki\Components\Util\Ajax;
67
use Loki\Components\Util\IsEmpty;
78

89
class RequiredValidator implements ValidatorInterface
910
{
1011
public function __construct(
11-
private IsEmpty $isEmpty,
12+
private readonly IsEmpty $isEmpty,
13+
private readonly Ajax $ajax,
1214
) {
1315
}
1416

1517
public function validate(mixed $value, ?ComponentInterface $component = null): bool|array
1618
{
19+
if (false === $this->ajax->isAjax()) {
20+
return true;
21+
}
22+
1723
if ($this->isEmpty->execute($component, $value)) {
1824
return [(string)__('Value is required')];
1925
}

Validator/Validator.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
namespace Loki\Components\Validator;
44

5-
use Loki\Components\Util\IsEmpty;
65
use Magento\Framework\Phrase;
76
use Loki\Components\Component\ComponentInterface;
87
use Loki\Components\Messages\LocalMessage;
9-
use Loki\Components\Util\Ajax;
108

119
class Validator
1210
{
1311
public function __construct(
1412
private readonly ValidatorRegistry $validatorRegistry,
15-
private readonly Ajax $ajax,
16-
private readonly IsEmpty $isEmpty,
1713
) {
1814
}
1915

@@ -22,17 +18,6 @@ public function validate(
2218
mixed $data = null,
2319
string $scope = ''
2420
): bool {
25-
// @todo: Move this to the RequiredValidator class
26-
if ($this->isRequired($component)
27-
&& $this->isEmpty->execute($component, $data)
28-
&& false === $this->ajax->isAjax()) {
29-
return true;
30-
}
31-
32-
if ($this->isRequired($component) && $this->isEmpty->execute($component, $data)) {
33-
return false;
34-
}
35-
3621
if (is_array($data)) {
3722
foreach ($data as $value) {
3823
if (false === $this->validate($component, $value)) {
@@ -74,9 +59,4 @@ public function validate(
7459

7560
return true;
7661
}
77-
78-
private function isRequired(ComponentInterface $component): bool
79-
{
80-
return in_array('required', $component->getValidators());
81-
}
8262
}

Validator/ValidatorRegistry.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public function __construct(
2323
*/
2424
public function getApplicableValidators(array $validators = []): array
2525
{
26+
$key = array_search('required', $validators);
27+
if ($key !== false) {
28+
unset($validators[$key]);
29+
array_unshift($validators, 'required');
30+
}
31+
2632
$applicableValidators = [];
2733
foreach ($validators as $validatorName) {
2834
if (array_key_exists($validatorName, $this->validators)) {

0 commit comments

Comments
 (0)