/** * {@inheritdoc} * * Expected input format : * { * 'attribute': 'maximum_print_size', * 'code': '210_x_1219_mm', * 'sort_order': 2, * 'labels': { * 'de_DE': '210 x 1219 mm', * 'en_US': '210 x 1219 mm', * 'fr_FR': '210 x 1219 mm' * } * } * * @throws BusinessValidationException */ public function update($attributeOption, array $data, array $options = []) { if (!$attributeOption instanceof AttributeOptionInterface) { throw new \InvalidArgumentException(sprintf('Expects a "Pim\\Bundle\\CatalogBundle\\Model\\AttributeOptionInterface", "%s" provided.', ClassUtils::getClass($attributeOption))); } // TODO: ugly fix to workaround issue with "attribute.group.code: This value should not be blank." // in case of existing option, attribute is a proxy, attribute group too, the validated group code is null $attributeOption->getAttribute() !== null ? $attributeOption->getAttribute()->getGroup()->getCode() : null; $isNew = $attributeOption->getId() === null; $readOnlyFields = ['attribute', 'code']; $updateViolations = new ConstraintViolationList(); foreach ($data as $field => $data) { $isReadOnlyField = in_array($field, $readOnlyFields); if ($isNew || !$isReadOnlyField) { try { $this->setData($attributeOption, $field, $data); } catch (\InvalidArgumentException $e) { $setViolation = new ConstraintViolation($e->getMessage(), $e->getMessage(), [], $attributeOption, null, null); $updateViolations->add($setViolation); } } } $validatorViolations = $this->validator->validate($attributeOption); $updateViolations->addAll($validatorViolations); if ($updateViolations->count() > 0) { throw new BusinessValidationException($updateViolations); } return $this; }
function it_validates_an_item_and_the_validation_fails_with_exception(ValidatorInterface $validator, Constraint $constraint, ConstraintViolationList $list) { $list->count()->willReturn(1); $validator->validate($this->item1, Argument::type('Symfony\\Component\\Validator\\Constraints\\Collection'))->willReturn($list); $this->throwExceptions(true); $this->add('key1', $constraint); $this->shouldThrow('Ddeboer\\DataImport\\Exception\\ValidationException')->during__invoke($this->item1); $this->getViolations()->shouldReturn([1 => $list]); }
/** * {@inheritdoc} */ public function validate($prices, array $options = [], $attributeCode) { $violations = new ConstraintViolationList(); foreach ($prices as $price) { if (isset($price['data']) && ($valid = parent::validate($price['data'], $options, $attributeCode))) { $violations->addAll($valid); } } return $violations->count() > 0 ? $violations : null; }
private function handleConstraintViolationList(ConstraintViolationList $violationList) { $errors = []; for ($i = 0; $i < $violationList->count(); $i++) { $errors['message'] = $violationList[$i]->getMessage(); $errors['field'] = $violationList[$i]->getPropertyPath(); $errors['code'] = $violationList[$i]->getCode(); } return $this->errorProcessor->processErrors($errors); }
function it_throws_exception_on_validation_failure(MethodInterface $method, ValidatorInterface $validator, ConstraintViolationList $violations) { $attributes = ['field' => 'value']; $constraints = ['constraints']; $method->getValidationConstraints()->shouldBeCalled()->willReturn($constraints); $method->getAttributes()->shouldBeCalled()->willReturn($attributes); $violations->count()->willReturn(1); $violations->__toString()->willReturn(''); $validator->validate($attributes, $constraints)->willReturn($violations); $this->shouldThrow('Cardinity\\Exception\\InvalidAttributeValue')->duringValidate($method); }
/** * Checks whether the given value is a valid URL that may probably be safely used as a redirect url. * * @param string $value URL to validate * * @return boolean true if the url is correct */ protected function isValidRedirectUrl($value) { $validator = Validation::createValidator(); $constraints = array(new Constraints\Type(array('type' => 'string')), new Constraints\Length(array('min' => $this->getParameter('min_length', 10), 'max' => $this->getParameter('max_length', 1000))), new Constraints\Url(array('protocols' => $this->getParameter('allowed_protocols', array('http', 'https'))))); if ($this->getParameter('check_base_href', true)) { $constraints[] = new Constraints\Callback(array('methods' => array(array($this, 'hasCorrectBaseHref')))); } $violations = new ConstraintViolationList(); foreach ($constraints as $constraint) { $violations->addAll($validator->validateValue($value, $constraint)); } if ($violations->count() === 0) { return true; } else { $this->getContext()->getLoggerManager()->logTo('default', \AgaviLogger::WARNING, __METHOD__, $violations->__toString()); return false; } }
/** * @param $userId * @param $workTimeId * @param $endDateRequest * @param $durationRequest * @param $descriptionRequest * @return WorkTimeDTO * @throws ValidationException * @throws WorkTimeNotActiveException * @throws WorkTimeNotFoundException */ public function stopWorkTime($userId, $workTimeId, $endDateRequest, $durationRequest, $descriptionRequest) { $workTime = $this->workTimeRepository->findWorkTimeByUserAndId($userId, $workTimeId); if ($workTime === null) { throw new WorkTimeNotFoundException(); } if ($workTime->getEndDate() !== null) { throw new WorkTimeNotActiveException(); } $validations = array(array($endDateRequest, array(new NotNull(), new DateTime()), 'endDate'), array($durationRequest, array(new NotNull(), new Regex(array('value' => '/([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]/'))), 'duration'), array($descriptionRequest, array(new Length(array('min' => 5, 'max' => '255'))))); $validationErrors = new ConstraintViolationList(); foreach ($validations as $values) { $errors = $this->validator->validate($values[0], $values[1]); if ($errors->count() > 0) { foreach ($errors as $error) { /* @var $error ConstraintViolationInterface */ $validationErrors->add(new ConstraintViolation($error->getMessage(), $error->getMessageTemplate(), $error->getMessageParameters(), $error->getRoot(), $values[2], $error->getInvalidValue())); } } } if ($validationErrors->count() > 0) { throw new ValidationException($validationErrors); } $duration = new \DateTime(); $durationRequest = explode(':', $durationRequest); $duration->setTime($durationRequest[0], $durationRequest[1], 0); $endDate = new \DateTime($endDateRequest); $this->workTimeRepository->stopWorkTime($workTime, $endDate, $duration, $descriptionRequest); return WorkTimeDTO::withEntity($workTime); }