Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validator.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Tax\Model\ClassModel as TaxClassModel;
13 
14 class Validator extends \Magento\Framework\Validator\AbstractValidator
15 {
20 
25  {
26  $this->classModelRegistry = $classModelRegistry;
27  }
28 
39  public function isValid($value)
40  {
41  $messages = [];
42 
43  // Position is required and must be 0 or greater
44  if (!\Zend_Validate::is(trim($value->getPosition()), 'NotEmpty')) {
45  $this->addErrorMessage(
46  $messages,
47  '"%fieldName" is required. Enter and try again.',
48  ['fieldName' => 'position']
49  );
50  }
51  if (!\Zend_Validate::is(trim($value->getPosition()), 'GreaterThan', [-1])) {
52  $this->addErrorMessage(
53  $messages,
54  'The %fieldName value of "%value" must be greater than or equal to %minValue.',
55  ['fieldName' => 'position', 'value' => $value->getPosition(), 'minValue' => 0]
56  );
57  }
58 
59  // Priority is required and must be 0 or greater
60  if (!\Zend_Validate::is(trim($value->getPriority()), 'NotEmpty')) {
61  $this->addErrorMessage(
62  $messages,
63  '"%fieldName" is required. Enter and try again.',
64  ['fieldName' => 'priority']
65  );
66  }
67  if (!\Zend_Validate::is(trim($value->getPriority()), 'GreaterThan', [-1])) {
68  $this->addErrorMessage(
69  $messages,
70  'The %fieldName value of "%value" must be greater than or equal to %minValue.',
71  ['fieldName' => 'priority', 'value' => $value->getPriority(), 'minValue' => 0]
72  );
73  }
74 
75  // Code is required
76  if (!\Zend_Validate::is(trim($value->getCode()), 'NotEmpty')) {
77  $this->addErrorMessage(
78  $messages,
79  '"%fieldName" is required. Enter and try again.',
80  ['fieldName' => 'code']
81  );
82  }
83 
84  // customer tax class ids is required
85  if (($value->getCustomerTaxClassIds() === null) || !$value->getCustomerTaxClassIds()) {
86  $this->addErrorMessage(
87  $messages,
88  '"%fieldName" is required. Enter and try again.',
89  ['fieldName' => 'customer_tax_class_ids']
90  );
91  } else { // see if the customer tax class ids exist
92  $customerTaxClassIds = $value->getCustomerTaxClassIds();
93  foreach ($customerTaxClassIds as $customerTaxClassId) {
94  try {
95  $taxClass = $this->classModelRegistry->retrieve($customerTaxClassId);
96  if ($taxClass === null || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_CUSTOMER)) {
97  $this->addErrorMessage(
98  $messages,
99  'No such entity with %fieldName = %fieldValue',
100  [
101  'fieldName' => 'customer_tax_class_ids',
102  'value' => $customerTaxClassId,
103  ]
104  );
105  }
106  } catch (NoSuchEntityException $e) {
107  $this->addErrorMessage(
108  $messages,
109  $e->getRawMessage(),
110  $e->getParameters()
111  );
112  }
113  }
114  }
115 
116  // product tax class ids is required
117  if (($value->getProductTaxClassIds() === null) || !$value->getProductTaxClassIds()) {
118  $this->addErrorMessage(
119  $messages,
120  '"%fieldName" is required. Enter and try again.',
121  ['fieldName' => 'product_tax_class_ids']
122  );
123  } else { // see if the product tax class ids exist
124  $productTaxClassIds = $value->getProductTaxClassIds();
125  foreach ($productTaxClassIds as $productTaxClassId) {
126  try {
127  $taxClass = $this->classModelRegistry->retrieve($productTaxClassId);
128  if ($taxClass === null || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_PRODUCT)) {
129  $this->addErrorMessage(
130  $messages,
131  'No such entity with %fieldName = %fieldValue',
132  [
133  'fieldName' => 'product_tax_class_ids',
134  'value' => $productTaxClassId,
135  ]
136  );
137  }
138  } catch (NoSuchEntityException $e) {
139  $this->addErrorMessage(
140  $messages,
141  $e->getRawMessage(),
142  $e->getParameters()
143  );
144  }
145  }
146  }
147 
148  // tax rate ids is required
149  if (($value->getTaxRateIds() === null) || !$value->getTaxRateIds()) {
150  $this->addErrorMessage(
151  $messages,
152  '"%fieldName" is required. Enter and try again.',
153  ['fieldName' => 'tax_rate_ids']
154  );
155  }
156  $this->_addMessages($messages);
157  return empty($messages);
158  }
159 
168  protected function addErrorMessage(&$messages, $message, $params)
169  {
170  $messages[$params['fieldName']] = __($message, $params);
171  }
172 }
__()
Definition: __.php:13
$message
addErrorMessage(&$messages, $message, $params)
Definition: Validator.php:168
$value
Definition: gender.phtml:16
__construct(ClassModelRegistry $classModelRegistry)
Definition: Validator.php:24
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18