public function handleExceptions(ConstraintViolationListInterface $violations)
 {
     if ($violations->count() > 0) {
         $violation = $violations->get(0);
         $this->throwValidationException($violation);
     }
 }
 /**
  * @param object $command
  * @param ConstraintViolationListInterface $violations
  * @return static
  */
 public static function onCommand($command, ConstraintViolationListInterface $violations)
 {
     $exception = new static('Validation failed for ' . get_class($command) . ' with ' . $violations->count() . ' violation(s).');
     $exception->command = $command;
     $exception->violations = $violations;
     return $exception;
 }
示例#3
0
 /**
  * @return int One of the result constants
  */
 public function handle(ConstraintViolationListInterface $constraintViolationList, Text $file)
 {
     $diff = null;
     if ($file instanceof File) {
         foreach ($this->diff as $d) {
             if (substr($d->getTo(), 2) === $file->getFilename()) {
                 $diff = $d;
                 break;
             }
         }
         if (null === $diff) {
             return Reporter::SUCCESS;
         }
     }
     foreach ($constraintViolationList as $offset => $violation) {
         /** @var ConstraintViolation $violation */
         $lineNumber = $this->getLineNumber($violation);
         foreach ($diff->getChunks() as $chunk) {
             if ($chunk->getEnd() > $lineNumber || $chunk->getEnd() + $chunk->getEndRange() < $lineNumber) {
                 $constraintViolationList->remove($offset);
             }
         }
     }
     return $this->reporter->handle($constraintViolationList, $file);
 }
 public function testWithGlobalEnabledFAilOnValidationShouldIgnoreErrorsWhenDisabledLocallly()
 {
     $converter = $this->getConverter(['fail_on_validation_error' => true, 'validation_errors_argument' => 'testValidationErrors']);
     $this->constraintViolations->method('count')->willReturn(7);
     $request = $this->getRequest();
     $configuration = $this->getConfiguration(false);
     $converter->apply($request, $configuration);
 }
 /**
  * Convert a localized attribute
  *
  * @param LocalizerInterface $localizer
  * @param array              $item
  * @param array              $options
  * @param string             $path
  *
  * @return array
  */
 protected function convertLocalizedToDefaultValue(LocalizerInterface $localizer, array $item, array $options, $path)
 {
     $violations = $localizer->validate($item['data'], $path, $options);
     if (null !== $violations) {
         $this->violations->addAll($violations);
     }
     $item['data'] = $localizer->delocalize($item['data'], $options);
     return $item;
 }
示例#6
0
 function it_should_not_save_an_invalid_cart($manager, $provider, $validator, CartEvent $event, CartInterface $cart, ConstraintViolationListInterface $constraintList)
 {
     $constraintList->count()->willReturn(1);
     $event->getCart()->willReturn($cart);
     $validator->validate($cart)->shouldBeCalled()->willReturn($constraintList);
     $manager->persist($cart)->shouldNotBeCalled();
     $manager->flush()->shouldNotBeCalled();
     $provider->setCart($cart)->shouldNotBeCalled();
     $this->saveCart($event);
 }
 /**
  * @param ConstraintViolationListInterface $violations
  * @param array                            $item
  *
  * @throws InvalidItemException
  */
 protected function manageViolations(ConstraintViolationListInterface $violations, array $item)
 {
     if ($violations->count() > 0) {
         $violationsMessage = '';
         foreach ($violations as $violation) {
             $violationsMessage .= PHP_EOL . $violation->getMessage();
         }
         $errorMessage = sprintf('Tag with code "%s" can\'t be process :%s', $item['code'], $violationsMessage);
         throw new InvalidItemException($errorMessage, $item);
     }
 }
示例#8
0
 public static function printViolations(ConstraintViolationListInterface $violations)
 {
     if ($violations->count() == 0) {
         echo "No violations\n";
     } else {
         /** @var ConstraintViolation $violation */
         foreach ($violations as $index => $violation) {
             echo $index . ' ' . $violation->getPropertyPath() . ': ' . $violation->getMessage() . "\n";
         }
     }
     echo "\n";
 }
示例#9
0
 public function validate($model)
 {
     $builder = new ValidatorBuilder();
     $builder->setTranslator($this->service->getTranslator());
     $validator = $builder->getValidator();
     $validations = $this->getValidations();
     $this->violations = new ConstraintViolationList();
     foreach ($validations as $column => $validation) {
         $method = 'get' . NameUtils::toStudlyCase($column);
         if (method_exists($model, $method)) {
             $value = $model->{$method}();
             $constraints = [];
             foreach ($validation as $options) {
                 $name = $options['constraint'];
                 unset($options['constraint']);
                 $constraints[] = $this->getConstraint($name, $options);
             }
             $violations = $validator->validate($value, $constraints);
             $this->violations->addAll($violations);
         }
     }
     return (bool) (!(count($this->violations) > 0));
 }
 /**
  * Configure job instance for uploaded file
  *
  * @param JobInstance $jobInstance
  * @param File        $file
  *
  * @return bool
  */
 protected function configureUploadJob(JobInstance $jobInstance, File $file)
 {
     $success = false;
     $job = $jobInstance->getJob();
     foreach ($job->getSteps() as $step) {
         if (method_exists($step, 'getReader')) {
             $reader = $step->getReader();
             if ($reader instanceof UploadedFileAwareInterface) {
                 $constraints = $reader->getUploadedFileConstraints();
                 $this->fileError = $this->getValidator()->validateValue($file, $constraints);
                 if ($this->fileError->count() !== 0) {
                     foreach ($this->fileError as $error) {
                         $this->addFlash('error', $error->getMessage());
                     }
                     return false;
                 } else {
                     $reader->setUploadedFile($file);
                     $success = true;
                 }
             }
         }
     }
     return $success;
 }
示例#11
0
 /**
  * {@inheritdoc}
  *
  * Override the '%uri' message parameter, to ensure that 'internal:' URIs
  * show a validation error message that doesn't mention that scheme.
  */
 public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state)
 {
     /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
     foreach ($violations as $offset => $violation) {
         $parameters = $violation->getParameters();
         if (isset($parameters['@uri'])) {
             $parameters['@uri'] = static::getUriAsDisplayableString($parameters['@uri']);
             $violations->set($offset, new ConstraintViolation($this->t($violation->getMessageTemplate(), $parameters), $violation->getMessageTemplate(), $parameters, $violation->getRoot(), $violation->getPropertyPath(), $violation->getInvalidValue(), $violation->getPlural(), $violation->getCode()));
         }
     }
     parent::flagErrors($items, $violations, $form, $form_state);
 }
示例#12
0
 public function __construct($message, ConstraintViolationListInterface $violations)
 {
     $message .= ' Violations: ' . $violations->__toString();
     parent::__construct($message);
     $this->violations = $violations;
 }
示例#13
0
 /**
  * {@inheritdoc}
  */
 public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
     if ($violations->count()) {
         // Locate the correct element in the form.
         $element = NestedArray::getValue($form_state->getCompleteForm(), $field_state['array_parents']);
         // Do not report entity-level validation errors if Form API errors have
         // already been reported for the field.
         // @todo Field validation should not be run on fields with FAPI errors to
         //   begin with. See https://www.drupal.org/node/2070429.
         $element_path = implode('][', $element['#parents']);
         if ($reported_errors = $form_state->getErrors()) {
             foreach (array_keys($reported_errors) as $error_path) {
                 if (strpos($error_path, $element_path) === 0) {
                     return;
                 }
             }
         }
         // Only set errors if the element is visible.
         if (Element::isVisibleElement($element)) {
             $handles_multiple = $this->handlesMultipleValues();
             $violations_by_delta = array();
             foreach ($violations as $violation) {
                 // Separate violations by delta.
                 $property_path = explode('.', $violation->getPropertyPath());
                 $delta = array_shift($property_path);
                 $violations_by_delta[$delta][] = $violation;
                 $violation->arrayPropertyPath = $property_path;
             }
             /** @var \Symfony\Component\Validator\ConstraintViolationInterface[] $delta_violations */
             foreach ($violations_by_delta as $delta => $delta_violations) {
                 // Pass violations to the main element:
                 // - if this is a multiple-value widget,
                 // - or if the violations are at the ItemList level.
                 if ($handles_multiple || !is_numeric($delta)) {
                     $delta_element = $element;
                 } else {
                     $original_delta = $field_state['original_deltas'][$delta];
                     $delta_element = $element[$original_delta];
                 }
                 foreach ($delta_violations as $violation) {
                     // @todo: Pass $violation->arrayPropertyPath as property path.
                     $error_element = $this->errorElement($delta_element, $violation, $form, $form_state);
                     if ($error_element !== FALSE) {
                         $form_state->setError($error_element, $violation->getMessage());
                     }
                 }
             }
         }
     }
 }
示例#14
0
 /**
  * Flags violations for the current form.
  *
  * @param \Symfony\Component\Validator\ConstraintViolationListInterface $violations
  *   The violations to flag.
  * @param array $form
  *   A nested array of form elements comprising the form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  */
 protected function flagViolations(ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
   // Manually flag violations of fields not handled by the form display.
   foreach ($violations->getIterator() as $violation) {
     if ($violation->getPropertyPath() == 'aspect_ratio') {
       $form_state->setErrorByName('aspect_ratio', $violation->getMessage());
     }
     if ($violation->getPropertyPath() == 'id') {
       $form_state->setErrorByName('id', $violation->getMessage());
     }
   }
 }