Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EavValidationRules.php
Go to the documentation of this file.
1 <?php
8 
10 
16 {
21  protected $validationRules = [
22  'email' => ['validate-email' => true],
23  'date' => ['validate-date' => true],
24  ];
25 
33  public function build(AbstractAttribute $attribute, array $data)
34  {
35  $validations = [];
36  if (isset($data['required']) && $data['required'] == 1) {
37  $validations = array_merge($validations, ['required-entry' => true]);
38  }
39  if ($attribute->getFrontendInput() === 'price') {
40  $validations = array_merge($validations, ['validate-zero-or-greater' => true]);
41  }
42  if ($attribute->getValidateRules()) {
43  $validations = array_merge($validations, $this->clipLengthRules($attribute->getValidateRules()));
44  }
45  return $this->aggregateRules($validations);
46  }
47 
52  private function aggregateRules(array $validations): array
53  {
54  $rules = [];
55  foreach ($validations as $type => $ruleValue) {
56  $rule = [$type => $ruleValue];
57  if ($type === 'input_validation') {
58  $rule = $this->validationRules[$ruleValue] ?? [];
59  }
60  if (count($rule) !== 0) {
61  $key = key($rule);
62  $rules[$key] = $rule[$key];
63  }
64  }
65  return $rules;
66  }
67 
72  private function clipLengthRules(array $rules): array
73  {
74  if (empty($rules['input_validation'])) {
75  unset(
76  $rules['min_text_length'],
77  $rules['max_text_length']
78  );
79  }
80  return $rules;
81  }
82 }
$type
Definition: item.phtml:13
build(AbstractAttribute $attribute, array $data)