addAll() public method

Merge an existing ConstraintViolationList into this list.
public addAll ( ConstraintViolationList $otherList )
$otherList ConstraintViolationList
Beispiel #1
0
 /**
  * Core request handler.
  *
  * @param GetResponseEvent $event The event
  *
  * @throws BadRequestHttpException
  * @throws UnsupportedMediaTypeHttpException
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     $controller = $event->getController();
     $request = $event->getRequest();
     $metadata = $this->getControllerActionMetadata($controller);
     if (null !== $metadata) {
         $violations = new ConstraintViolationList();
         if (0 < count($metadata->queryParams)) {
             // set defaults
             $this->setDefaultValues($metadata->queryParams, $request->query);
             $queryViolations = $this->validateParams($this->container->get('validator'), $metadata->queryParams, $request->query);
             $violations->addAll($queryViolations);
         }
         if (0 < count($metadata->requestParams)) {
             // set defaults
             $this->setDefaultValues($metadata->requestParams, $request->request);
             $requestViolations = $this->validateParams($this->container->get('validator'), $metadata->requestParams, $request->request);
             $violations->addAll($requestViolations);
         }
         $violationParam = $this->getViolationsParameterName($metadata);
         if (null !== $violationParam) {
             // if action has an argument for violations, pass it
             $request->attributes->set($violationParam, $violations);
         } elseif (0 < count($violations)) {
             // if action has no arg for violations and there is at least one, throw an exception
             throw new ValidationException($violations);
         }
     }
 }
 /**
  * {@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;
 }
 /**
  * {@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;
 }
 function it_returns_a_constraint_if_the_decimal_separator_is_not_valid($validator)
 {
     $constraintUSD = new ConstraintViolation('Error on number validator', '', [], '', '', '');
     $constraintEUR = new ConstraintViolation('Error on number validator', '', [], '', '', '');
     $constraints = new ConstraintViolationList([$constraintEUR, $constraintUSD]);
     $validator->validate('10,00', Argument::any())->willReturn($constraints);
     $validator->validate('10,05', Argument::any())->willReturn(null);
     $prices = [['data' => '10,00', 'currency' => 'EUR'], ['data' => '10,05', 'currency' => 'USD']];
     $allConstraints = new ConstraintViolationList();
     $allConstraints->addAll($constraints);
     $this->validate($prices, ['decimal_separator' => ','], 'prices')->shouldHaveCount(2);
 }
 /**
  * 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;
     }
 }
Beispiel #6
0
 /**
  * Validates the object and all objects related to this table.
  *
  * @see        getValidationFailures()
  * @param      ValidatorInterface|null $validator A Validator class instance
  * @return     boolean Whether all objects pass validation.
  */
 public function validate(ValidatorInterface $validator = null)
 {
     if (null === $validator) {
         $validator = new RecursiveValidator(new ExecutionContextFactory(new IdentityTranslator()), new LazyLoadingMetadataFactory(new StaticMethodLoader()), new ConstraintValidatorFactory());
     }
     $failureMap = new ConstraintViolationList();
     if (!$this->alreadyInValidation) {
         $this->alreadyInValidation = true;
         $retval = null;
         // We call the validate method on the following object(s) if they
         // were passed to this object by their corresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         // If validate() method exists, the validate-behavior is configured for related object
         if (method_exists($this->aOwner, 'validate')) {
             if (!$this->aOwner->validate($validator)) {
                 $failureMap->addAll($this->aOwner->getValidationFailures());
             }
         }
         $retval = $validator->validate($this);
         if (count($retval) > 0) {
             $failureMap->addAll($retval);
         }
         if (null !== $this->collPackPermissions) {
             foreach ($this->collPackPermissions as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         if (null !== $this->collUserGroups) {
             foreach ($this->collUserGroups as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         $this->alreadyInValidation = false;
     }
     $this->validationFailures = $failureMap;
     return (bool) (!(count($this->validationFailures) > 0));
 }
 /**
  * {@inheritdoc}
  */
 public function validateContexts()
 {
     $violations = new ConstraintViolationList();
     // @todo: Implement symfony validator API to let the validator traverse
     // and set property paths accordingly.
     foreach ($this->getContexts() as $context) {
         $violations->addAll($context->validate());
     }
     return $violations;
 }
 /**
  * Validates the object and all objects related to this table.
  *
  * @see        getValidationFailures()
  * @param      object $validator A Validator class instance
  * @return     boolean Whether all objects pass validation.
  */
 public function validate(ValidatorInterface $validator = null)
 {
     if (null === $validator) {
         if (class_exists('Symfony\\Component\\Validator\\Validator\\LegacyValidator')) {
             $validator = new LegacyValidator(new ExecutionContextFactory(new DefaultTranslator()), new ClassMetaDataFactory(new StaticMethodLoader()), new ConstraintValidatorFactory());
         } else {
             $validator = new Validator(new ClassMetadataFactory(new StaticMethodLoader()), new ConstraintValidatorFactory(), new DefaultTranslator());
         }
     }
     $failureMap = new ConstraintViolationList();
     if (!$this->alreadyInValidation) {
         $this->alreadyInValidation = true;
         $retval = null;
         // We call the validate method on the following object(s) if they
         // were passed to this object by their corresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         // If validate() method exists, the validate-behavior is configured for related object
         if (method_exists($this->aInterested, 'validate')) {
             if (!$this->aInterested->validate($validator)) {
                 $failureMap->addAll($this->aInterested->getValidationFailures());
             }
         }
         // If validate() method exists, the validate-behavior is configured for related object
         if (method_exists($this->aTarget_Event, 'validate')) {
             if (!$this->aTarget_Event->validate($validator)) {
                 $failureMap->addAll($this->aTarget_Event->getValidationFailures());
             }
         }
         $retval = $validator->validate($this);
         if (count($retval) > 0) {
             $failureMap->addAll($retval);
         }
         $this->alreadyInValidation = false;
     }
     $this->validationFailures = $failureMap;
     return (bool) (!(count($this->validationFailures) > 0));
 }
Beispiel #9
0
 /**
  * Validates the object and all objects related to this table.
  *
  * @see        getValidationFailures()
  * @param      ValidatorInterface|null $validator A Validator class instance
  * @return     boolean Whether all objects pass validation.
  */
 public function validate(ValidatorInterface $validator = null)
 {
     if (null === $validator) {
         $validator = new RecursiveValidator(new ExecutionContextFactory(new IdentityTranslator()), new LazyLoadingMetadataFactory(new StaticMethodLoader()), new ConstraintValidatorFactory());
     }
     $failureMap = new ConstraintViolationList();
     if (!$this->alreadyInValidation) {
         $this->alreadyInValidation = true;
         $retval = null;
         $retval = $validator->validate($this);
         if (count($retval) > 0) {
             $failureMap->addAll($retval);
         }
         if (null !== $this->collPermissionGroupUsers) {
             foreach ($this->collPermissionGroupUsers as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         if (null !== $this->collLyrics) {
             foreach ($this->collLyrics as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         if (null !== $this->collLyricTranslations) {
             foreach ($this->collLyricTranslations as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         if (null !== $this->collLyricVotes) {
             foreach ($this->collLyricVotes as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         if (null !== $this->collArtists) {
             foreach ($this->collArtists as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         if (null !== $this->collAlbums) {
             foreach ($this->collAlbums as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         $this->alreadyInValidation = false;
     }
     $this->validationFailures = $failureMap;
     return (bool) (!(count($this->validationFailures) > 0));
 }
Beispiel #10
0
 /**
  * Controller.
  *
  * @param FilterControllerEvent $event The event
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     $request = $this->request = $event->getRequest();
     if ($request->attributes->has('start') && $request->attributes->has('count')) {
         return;
     }
     if (false === $this->isEnabled()) {
         return;
     }
     $controller = $event->getController();
     if (false === in_array($request->getMethod(), array('GET', 'HEAD', 'DELETE'))) {
         // pagination only makes sense with GET or DELETE methods
         return;
     }
     $metadata = $this->getControllerActionMetadata($controller);
     if (null === $metadata || null === $metadata->default_start) {
         // no annotations defined for this controller
         return;
     }
     $start = $metadata->default_start;
     $count = $metadata->default_count;
     $range = explode(',', $request->headers->get('Range'));
     if (true === isset($range[0])) {
         $start = intval($range[0]);
     }
     if (true === isset($range[1])) {
         $count = intval($range[1]);
     }
     $violations = new ConstraintViolationList();
     $start_violations = $this->validator->validateValue($start, array(new \Symfony\Component\Validator\Constraints\Type(array('type' => 'numeric', 'message' => 'Headers "Range" attribute first operand (=start) must be a positive integer')), new \Symfony\Component\Validator\Constraints\Range(array('min' => 0, 'minMessage' => 'Headers "Range" attribute first operand (=start) must be a positive integer'))));
     $count_label = 'Headers "Range" attribute second operand (=count)';
     $count_violations = $this->validator->validateValue($count, array(new \Symfony\Component\Validator\Constraints\Type(array('type' => 'numeric', 'message' => "{$count_label} must be a positive integer")), new \Symfony\Component\Validator\Constraints\Range(array('min' => $metadata->min_count, 'minMessage' => "{$count_label} must be greater than or equal to " . $metadata->min_count, 'max' => $metadata->max_count, 'maxMessage' => "{$count_label} must be less than or equal to " . $metadata->max_count))));
     $violations->addAll($start_violations);
     $violations->addAll($count_violations);
     $violation_param = $this->getViolationsParameterName($metadata);
     if (null !== $violation_param) {
         // if action has an argument for violations, pass it
         $request->attributes->set($violation_param, $violations);
     } elseif (0 < count($violations)) {
         // if action has no arg for violations and there is at least one, throw an exception
         throw new ValidationException($violations);
     }
     // add pagination properties to attributes
     $request->attributes->set('start', $start);
     $request->attributes->set('count', $count);
     // remove pagination properties from headers
     $request->headers->remove('Range');
 }
Beispiel #11
0
 /**
  * Validates an object using fields validators
  *
  * @param object $object
  */
 public function validateObject($object)
 {
     if ($this->validationMethod === self::VALIDATE_WITH_METHOD) {
         if (!method_exists($object, 'validate')) {
             return true;
         }
         if (!call_user_func(array($object, 'validate'))) {
             $violations = implode("\n", $object->getValidationFailures());
             throw new ValidationException($violations);
         }
         return true;
     }
     if ($this->validationMethod === self::VALIDATE_WITH_CALLBACK) {
         if ($this->validator === null) {
             throw new DefinitionBuilderException("Missing validation callback");
         }
         return call_user_func($this->validator, array($object, $this));
     }
     if ($this->validator === null) {
         return true;
     }
     if ($this->className !== null) {
         $violiations = $this->validator->validate($object);
     } else {
         $violiations = new ConstraintViolationList();
         foreach ($this->fields as $field) {
             $value = $field->getValue($object);
             $violiations->addAll($this->validator->validateValue($value, $field->getConstraints()));
         }
     }
     if (count($violiations)) {
         throw new ValidationException($violiations);
     }
     return true;
 }
 /**
  * Validates the object and all objects related to this table.
  *
  * @see        getValidationFailures()
  * @param      object $validator A Validator class instance
  * @return     boolean Whether all objects pass validation.
  */
 public function validate(Validator $validator = null)
 {
     if (null === $validator) {
         $validator = new Validator(new ClassMetadataFactory(new StaticMethodLoader()), new ConstraintValidatorFactory(), new DefaultTranslator());
     }
     $failureMap = new ConstraintViolationList();
     if (!$this->alreadyInValidation) {
         $this->alreadyInValidation = true;
         $retval = null;
         // We call the validate method on the following object(s) if they
         // were passed to this object by their corresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         // If validate() method exists, the validate-behavior is configured for related object
         if (method_exists($this->aDiaporama, 'validate')) {
             if (!$this->aDiaporama->validate($validator)) {
                 $failureMap->addAll($this->aDiaporama->getValidationFailures());
             }
         }
         $retval = $validator->validate($this);
         if (count($retval) > 0) {
             $failureMap->addAll($retval);
         }
         if (null !== $this->collDiaporamaImageI18ns) {
             foreach ($this->collDiaporamaImageI18ns as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         $this->alreadyInValidation = false;
     }
     $this->validationFailures = $failureMap;
     return (bool) (!(count($this->validationFailures) > 0));
 }
 /**
  * Validates the object and all objects related to this table.
  *
  * @see        getValidationFailures()
  * @param      object $validator A Validator class instance
  * @return     boolean Whether all objects pass validation.
  */
 public function validate(ValidatorInterface $validator = null)
 {
     if (null === $validator) {
         if (class_exists('Symfony\\Component\\Validator\\Validator\\LegacyValidator')) {
             $validator = new LegacyValidator(new ExecutionContextFactory(new DefaultTranslator()), new ClassMetaDataFactory(new StaticMethodLoader()), new ConstraintValidatorFactory());
         } else {
             $validator = new Validator(new ClassMetadataFactory(new StaticMethodLoader()), new ConstraintValidatorFactory(), new DefaultTranslator());
         }
     }
     $failureMap = new ConstraintViolationList();
     if (!$this->alreadyInValidation) {
         $this->alreadyInValidation = true;
         $retval = null;
         $retval = $validator->validate($this);
         if (count($retval) > 0) {
             $failureMap->addAll($retval);
         }
         if (null !== $this->collEvents) {
             foreach ($this->collEvents as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         if (null !== $this->collInterests) {
             foreach ($this->collInterests as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         if (null !== $this->collComments) {
             foreach ($this->collComments as $referrerFK) {
                 if (method_exists($referrerFK, 'validate')) {
                     if (!$referrerFK->validate($validator)) {
                         $failureMap->addAll($referrerFK->getValidationFailures());
                     }
                 }
             }
         }
         $this->alreadyInValidation = false;
     }
     $this->validationFailures = $failureMap;
     return (bool) (!(count($this->validationFailures) > 0));
 }