getRoot example

if ($violation instanceof ConstraintViolation) {
        $constraint = $violation->getConstraint();
        $cause = $violation->getCause();
        $parameters = $violation->getParameters();
        $plural = $violation->getPlural();
      }

      $new_violation = new ConstraintViolation(
        $violation->getMessage(),
        $violation->getMessageTemplate(),
        $parameters,
        $violation->getRoot(),
        $new_path,
        $violation->getInvalidValue(),
        $plural,
        $violation->getCode(),
        $constraint,
        $cause
      );
      $new_violations->add($new_violation);
    }
    return $new_violations;
  }

  
private function assertViolationEquals(ConstraintViolation $expectedViolation)
    {
        $this->assertCount(1, $this->violations);

        $violation = $this->violations->get(0);

        $this->assertSame($expectedViolation->getMessage()$violation->getMessage());
        $this->assertSame($expectedViolation->getMessageTemplate()$violation->getMessageTemplate());
        $this->assertSame($expectedViolation->getParameters()$violation->getParameters());
        $this->assertSame($expectedViolation->getPlural()$violation->getPlural());
        $this->assertSame($expectedViolation->getRoot()$violation->getRoot());
        $this->assertSame($expectedViolation->getPropertyPath()$violation->getPropertyPath());
        $this->assertSame($expectedViolation->getInvalidValue()$violation->getInvalidValue());
        $this->assertSame($expectedViolation->getCode()$violation->getCode());
        $this->assertEquals($expectedViolation->getConstraint()$violation->getConstraint());
        $this->assertSame($expectedViolation->getCause()$violation->getCause());
    }
}

    public function validate($email, Constraint $constraint)
    {
        if (!$constraint instanceof FormEmail) {
            return;
        }

        /** @var FormInterface<Customer> $form */
        $form = $this->context->getRoot();

        $customer = $form->getData();

        $accountMode = $this->getAccountMode($form);

        $emailConstraint = new CustomerEmail([
            'shop' => $constraint->getShop(),
            'customerId' => $customer->getId(),
            'accountMode' => $accountMode,
        ]);

        
        if ($violation->getConstraint() instanceof CompositeConstraintBase) {
          $covered_fields = $violation->getConstraint()->coversFields();

          // Keep the composite field if it covers some remaining field and put           // a violation on some other covered field instead.           if ($remaining_fields = array_diff($covered_fields$field_names)) {
            $message_params = ['%field_name' => $field_name];
            $violation = new ConstraintViolation(
              $this->t('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', $message_params),
              'The validation failed because the value conflicts with the value in %field_name, which you cannot access.',
              $message_params,
              $violation->getRoot(),
              reset($remaining_fields),
              $violation->getInvalidValue(),
              $violation->getPlural(),
              $violation->getCode(),
              $violation->getConstraint(),
              $violation->getCause()
            );
            $new_violations[] = $violation;
          }
        }

        
/** * @covers ::getName */
  public function testGetName() {
    $this->assertNull($this->entityAdapter->getName());
  }

  /** * @covers ::getRoot */
  public function testGetRoot() {
    $this->assertSame(spl_object_hash($this->entityAdapter)spl_object_hash($this->entityAdapter->getRoot()));
  }

  /** * @covers ::getPropertyPath */
  public function testGetPropertyPath() {
    $this->assertSame('', $this->entityAdapter->getPropertyPath());
  }

  /** * @covers ::getParent */

  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($offsetnew 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);
  }

}
$property = trim($property, '][');
                    $fieldName .= '/' . $property;
                }

                $fieldName = '/' . $fieldName;

                $violationList->add(
                    new ConstraintViolation(
                        $violation->getMessage(),
                        $violation->getMessageTemplate(),
                        $violation->getParameters(),
                        $violation->getRoot(),
                        $fieldName,
                        $violation->getInvalidValue(),
                        $violation->getPlural(),
                        $violation->getCode(),
                        $violation->getConstraint(),
                        $violation->getCause()
                    )
                );
            }
        }

        
Assert::assertEquals($violationcurrent($violations));
            next($violations);
        }
    }

    private function getViolation(): ConstraintViolation
    {
        return new ConstraintViolation(
            $this->message,
            $this->message,
            $this->parameters,
            $this->context->getRoot(),
            $this->propertyPath,
            $this->invalidValue,
            $this->plural,
            $this->code,
            $this->constraint,
            $this->cause
        );
    }
}

/** * @internal */
/** * {@inheritdoc} * * @throws \Symfony\Component\Validator\Exception\UnexpectedTypeException * Thrown when the given constraint is not supported by this validator. */
  public function validate($toolbar_item, Constraint $constraint) {
    if (!$constraint instanceof ToolbarItemDependencyConstraint) {
      throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\ToolbarItemDependency');
    }

    $toolbar_items = $this->context->getRoot()->get('settings.toolbar.items')->toArray();
    if (!in_array($constraint->toolbarItem, $toolbar_items, TRUE)) {
      $this->context->buildViolation($constraint->message)
        ->setParameter('%toolbar_item', $constraint->toolbarItem)
        ->addViolation();
    }
  }

}
static::assertNotNull($error);
        static::assertInstanceOf(AddressValidationError::class$error);
        static::assertTrue($error->isBillingAddress());

        static::assertCount(1, $error->getViolations());

        $violation = $error->getViolations()->get(0);

        static::assertNotNull($violation);
        static::assertInstanceOf(ConstraintViolation::class$violation);
        static::assertSame('Test error', $violation->getMessage());
        static::assertSame('root', $violation->getRoot());
        static::assertSame('invalidValue', $violation->getInvalidValue());
    }

    public function testViolationsAreAddedAsCartErrorsWithDifferentAddresses(): void
    {
        $violations = new ConstraintViolationList([
            new ConstraintViolation(
                'Test error',
                null,
                [],
                'root',
                
$violations = $typed_data->validate();
    $this->assertEquals(0, $violations->count(), 'Validation passed for correct value.');

    // Test the validation when an invalid value is passed.     $typed_data = $this->typedData->create($definition, 4);
    $violations = $typed_data->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');

    // Make sure the information provided by a violation is correct.     $violation = $violations[0];
    $this->assertEquals('The value you selected is not a valid choice.', $violation->getMessage(), 'The message for invalid value is correct.');
    $this->assertEquals($typed_data$violation->getRoot(), 'Violation root is correct.');
    $this->assertEquals(4, $violation->getInvalidValue(), 'The invalid value is set correctly in the violation.');

    // Test the validation when a value of an incorrect type is passed.     $typed_data = $this->typedData->create($definition, '1');
    $violations = $typed_data->validate();
    $this->assertEquals(0, $violations->count(), 'Value is coerced to the correct type and is valid.');
  }

  /** * Tests the AllowedValuesConstraintValidator with callbacks. */
  


            $translationDomain ??= $view->parent->vars['translation_domain'];

            $labelTranslationParameters = array_merge($view->parent->vars['label_translation_parameters']$labelTranslationParameters);
            $attrTranslationParameters = array_merge($view->parent->vars['attr_translation_parameters']$attrTranslationParameters);

            if (!$labelFormat) {
                $labelFormat = $view->parent->vars['label_format'];
            }

            $rootFormAttrOption = $form->getRoot()->getConfig()->getOption('form_attr');
            if ($options['form_attr'] || $rootFormAttrOption) {
                $options['attr']['form'] = \is_string($rootFormAttrOption) ? $rootFormAttrOption : $form->getRoot()->getName();
                if (empty($options['attr']['form'])) {
                    throw new LogicException('"form_attr" option must be a string identifier on root form when it has no id.');
                }
            }
        } else {
            $id = \is_string($options['form_attr']) ? $options['form_attr'] : $name;
            $fullName = $name;
            $uniqueBlockPrefix = '_'.$blockName;

            
$allowed_values = $data->getSettableValues($user);
    $this->assertEquals(['plain_text']$allowed_values);
    $allowed_options = $data->getSettableOptions($user);
    $this->assertEquals(['plain_text' => 'Plain text']$allowed_options);

    $data->setValue('foo');
    $violations = $data->validate();
    $this->assertFilterFormatViolation($violations, 'foo');

    // Make sure the information provided by a violation is correct.     $violation = $violations[0];
    $this->assertEquals($data$violation->getRoot(), 'Violation root is filter format.');
    $this->assertEquals('', $violation->getPropertyPath(), 'Violation property path is correct.');
    $this->assertEquals('foo', $violation->getInvalidValue(), 'Violation contains invalid value.');

    $data->setValue('plain_text');
    $violations = $data->validate();
    $this->assertCount(0, $violations, "No validation violation for format 'plain_text' found");

    // Anonymous doesn't have access to the 'filtered_html' format.     $data->setValue('filtered_html');
    $violations = $data->validate();
    $this->assertFilterFormatViolation($violations, 'filtered_html');

    
private function mapErrorCodes(ConstraintViolationList $violations): void
    {
        /** @var ConstraintViolation $violation */
        foreach ($violations as $key => $violation) {
            if ($constraint = $violation->getConstraint()) {
                $violations->remove($key);
                $violations->add(new ConstraintViolation(
                    $violation->getMessage(),
                    $violation->getMessageTemplate(),
                    $violation->getParameters(),
                    $violation->getRoot(),
                    $violation->getPropertyPath(),
                    $violation->getInvalidValue(),
                    $violation->getPlural(),
                    'VIOLATION::' . $constraint->getErrorName($violation->getCode() ?? ''),
                    $constraint
                ));
            }
        }
    }
}
// Test the validation when an invalid value (in this case a user entity)     // is passed.     $account = $this->createUser();

    $typed_data = $this->typedData->create($definition$account);
    $violations = $typed_data->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');

    // Make sure the information provided by a violation is correct.     $violation = $violations[0];
    $this->assertEquals(t('The entity must be of type %type.', ['%type' => $entity_type])$violation->getMessage(), 'The message for invalid value is correct.');
    $this->assertEquals($typed_data$violation->getRoot(), 'Violation root is correct.');
    $this->assertEquals($account$violation->getInvalidValue(), 'The invalid value is set correctly in the violation.');
  }

}
Home | Imprint | This part of the site doesn't use cookies.