예제 #1
0
 /**
  * Validate input data against validation rules, defined in config group.
  *
  * @param array $data
  * @throws Magento_Exception
  * @return bool
  */
 public function isValid(array $data)
 {
     $isValid = true;
     $rules = $this->_config->getValidationRules($this->_entityName, $this->_groupName);
     foreach ($rules as $rule) {
         foreach ($rule as $constraintConfig) {
             $constraint = $constraintConfig['constraint'];
             $field = isset($constraintConfig['field']) ? $constraintConfig['field'] : null;
             if ($constraint instanceof Zend_Validate_Interface) {
                 /** @var Zend_Validate_Interface $constraint */
                 $value = isset($data[$field]) ? $data[$field] : null;
                 if (!$constraint->isValid($value)) {
                     foreach ($constraint->getMessages() as $error) {
                         $this->_messages[$field][] = $error;
                     }
                     $isValid = false;
                 }
             } else {
                 /** @var Magento_Validator_ConstraintAbstract $constraint */
                 if (!$constraint->isValidData($data, $field)) {
                     foreach ($constraint->getErrors() as $errorFieldName => $errors) {
                         foreach ($errors as $error) {
                             $this->_messages[$errorFieldName][] = $error;
                         }
                     }
                     $isValid = false;
                 }
             }
         }
     }
     return $isValid;
 }
예제 #2
0
 /**
  * @dataProvider getValidationRulesDataProvider
  * @param string $entityName
  * @param string $groupName
  * @param array $expectedRules
  */
 public function testGetValidationRules($entityName, $groupName, $expectedRules)
 {
     $actualRules = self::$_model->getValidationRules($entityName, $groupName);
     $this->assertRulesEqual($expectedRules, $actualRules);
 }