|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | + |
6 | 7 | namespace Magento\Ui\DataProvider;
|
7 | 8 |
|
8 | 9 | use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
|
@@ -31,25 +32,51 @@ class EavValidationRules
|
31 | 32 | */
|
32 | 33 | public function build(AbstractAttribute $attribute, array $data)
|
33 | 34 | {
|
34 |
| - $validation = []; |
| 35 | + $validations = []; |
35 | 36 | if (isset($data['required']) && $data['required'] == 1) {
|
36 |
| - $validation = array_merge($validation, ['required-entry' => true]); |
| 37 | + $validations = array_merge($validations, ['required-entry' => true]); |
37 | 38 | }
|
38 | 39 | if ($attribute->getFrontendInput() === 'price') {
|
39 |
| - $validation = array_merge($validation, ['validate-zero-or-greater' => true]); |
| 40 | + $validations = array_merge($validations, ['validate-zero-or-greater' => true]); |
40 | 41 | }
|
41 | 42 | if ($attribute->getValidateRules()) {
|
42 |
| - $validation = array_merge($validation, $attribute->getValidateRules()); |
| 43 | + $validations = array_merge($validations, $this->clipLengthRules($attribute->getValidateRules())); |
43 | 44 | }
|
| 45 | + return $this->aggregateRules($validations); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param array $validations |
| 50 | + * @return array |
| 51 | + */ |
| 52 | + protected function aggregateRules(array $validations): array |
| 53 | + { |
44 | 54 | $rules = [];
|
45 |
| - foreach ($validation as $type => $ruleName) { |
46 |
| - $rule = [$type => $ruleName]; |
| 55 | + foreach ($validations as $type => $ruleValue) { |
| 56 | + $rule = [$type => $ruleValue]; |
47 | 57 | if ($type === 'input_validation') {
|
48 |
| - $rule = isset($this->validationRules[$ruleName]) ? $this->validationRules[$ruleName] : []; |
| 58 | + $rule = $this->validationRules[$ruleValue] ?? []; |
| 59 | + } |
| 60 | + if (count($rule) !== 0) { |
| 61 | + $key = key($rule); |
| 62 | + $rules[$key] = $rule[$key]; |
49 | 63 | }
|
50 |
| - $rules = array_merge($rules, $rule); |
51 | 64 | }
|
| 65 | + return $rules; |
| 66 | + } |
52 | 67 |
|
| 68 | + /** |
| 69 | + * @param array $rules |
| 70 | + * @return array |
| 71 | + */ |
| 72 | + private function clipLengthRules(array $rules): array |
| 73 | + { |
| 74 | + if (empty($validateRules['input_validation'])) { |
| 75 | + unset( |
| 76 | + $rules['min_text_length'], |
| 77 | + $rules['max_text_length'] |
| 78 | + ); |
| 79 | + } |
53 | 80 | return $rules;
|
54 | 81 | }
|
55 | 82 | }
|
0 commit comments