Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataObject.php
Go to the documentation of this file.
1 <?php
12 
18 {
24  private $_rules = [];
25 
31  private $_messages = [];
32 
41  public function addRule(\Zend_Validate_Interface $validator, $fieldName = '')
42  {
43  if (!array_key_exists($fieldName, $this->_rules)) {
44  $this->_rules[$fieldName] = $validator;
45  } else {
46  $existingValidator = $this->_rules[$fieldName];
47  if (!$existingValidator instanceof \Zend_Validate) {
48  $compositeValidator = new \Zend_Validate();
49  $compositeValidator->addValidator($existingValidator);
50  $this->_rules[$fieldName] = $compositeValidator;
51  }
52  $this->_rules[$fieldName]->addValidator($validator);
53  }
54  return $this;
55  }
56 
66  public function isValid($entity)
67  {
68  $this->_messages = [];
70  foreach ($this->_rules as $fieldName => $validator) {
71  $value = $fieldName ? $entity->getDataUsingMethod($fieldName) : $entity;
72  if (!$validator->isValid($value)) {
73  $this->_messages = array_merge($this->_messages, array_values($validator->getMessages()));
74  }
75  }
76  return empty($this->_messages);
77  }
78 
84  public function getMessages()
85  {
86  return $this->_messages;
87  }
88 }
$value
Definition: gender.phtml:16
addRule(\Zend_Validate_Interface $validator, $fieldName='')
Definition: DataObject.php:41
$entity
Definition: element.phtml:22