Skip to content

Commit c576ec9

Browse files
committed
Make sure LengthValidator also reads from min / max
1 parent e358210 commit c576ec9

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

Validator/LengthValidator.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Loki\Components\Component\ComponentInterface;
66
use Loki\Components\Component\ComponentViewModelInterface;
7+
use Magento\Framework\View\Element\AbstractBlock;
78

89
class LengthValidator implements ValidatorInterface
910
{
@@ -18,16 +19,50 @@ public function validate(mixed $value, ?ComponentInterface $component = null): b
1819
return true;
1920
}
2021

21-
$minlength = (int)$viewModel->getBlock()->getMinlength();
22+
$minlength = $this->getMin($viewModel->getBlock());
2223
if ($minlength > 0 && strlen($value) < $minlength) {
2324
return [__('Value should have more than %1 characters', $minlength)];
2425
}
2526

26-
$maxlength = (int)$viewModel->getBlock()->getMaxlength();
27+
$maxlength = $this->getMax($viewModel->getBlock());
2728
if ($maxlength > 0 && strlen($value) > $maxlength) {
2829
return [__('Value should have no more than %1 characters', $maxlength)];
2930
}
3031

3132
return true;
3233
}
34+
35+
private function getMin(AbstractBlock $block): int
36+
{
37+
$possibleValues = ['min', 'minlength'];
38+
foreach ($possibleValues as $possibleValue) {
39+
if ((int)$block->getData($possibleValue) > 0) {
40+
return (int)$block->getData($possibleValue);
41+
}
42+
43+
$fieldAttributes = (array)$block->getFieldAttributes();
44+
if (!empty($fieldAttributes[$possibleValue])) {
45+
return $fieldAttributes[$possibleValue];
46+
}
47+
}
48+
49+
return 0;
50+
}
51+
52+
private function getMax(AbstractBlock $block): int
53+
{
54+
$possibleValues = ['max', 'maxlength'];
55+
foreach ($possibleValues as $possibleValue) {
56+
if ((int)$block->getData($possibleValue) > 0) {
57+
return (int)$block->getData($possibleValue);
58+
}
59+
60+
$fieldAttributes = (array)$block->getFieldAttributes();
61+
if (!empty($fieldAttributes[$possibleValue])) {
62+
return $fieldAttributes[$possibleValue];
63+
}
64+
}
65+
66+
return 0;
67+
}
3368
}

0 commit comments

Comments
 (0)