inContext example


        if (!$constraint instanceof Valid) {
            throw new UnexpectedTypeException($constraint, Valid::class);
        }

        if (null === $value) {
            return;
        }

        $this->context
            ->getValidator()
            ->inContext($this->context)
            ->validate($value, null, $this->context->getGroup());
    }
}
$entity->reference = new Reference();

        $callback1 = function D$value, ExecutionContextInterface $context) {
            $previousValue = $context->getValue();
            $previousObject = $context->getObject();
            $previousMetadata = $context->getMetadata();
            $previousPath = $context->getPropertyPath();
            $previousGroup = $context->getGroup();

            $context
                ->getValidator()
                ->inContext($context)
                ->atPath('subpath')
                ->validate($value->reference)
            ;

            // context changes shouldn't leak out of the validate() call             $this->assertSame($previousValue$context->getValue());
            $this->assertSame($previousObject$context->getObject());
            $this->assertSame($previousMetadata$context->getMetadata());
            $this->assertSame($previousPath$context->getPropertyPath());
            $this->assertSame($previousGroup$context->getGroup());
        };

        
/** * @return void */
    public function validate(mixed $value, Constraint $constraint)
    {
        if (!$constraint instanceof Sequentially) {
            throw new UnexpectedTypeException($constraint, Sequentially::class);
        }

        $context = $this->context;

        $validator = $context->getValidator()->inContext($context);

        $originalCount = $validator->getViolations()->count();

        foreach ($constraint->constraints as $c) {
            if ($originalCount !== $validator->validate($value$c)->getViolations()->count()) {
                break;
            }
        }
    }
}
if (!$formConstraint instanceof Form) {
            throw new UnexpectedTypeException($formConstraint, Form::class);
        }

        if (!$form instanceof FormInterface) {
            return;
        }

        /* @var FormInterface $form */
        $config = $form->getConfig();

        $validator = $this->context->getValidator()->inContext($this->context);

        if ($form->isSubmitted() && $form->isSynchronized()) {
            // Validate the form data only if transformation succeeded             $groups = $this->getValidationGroups($form);

            if (!$groups) {
                return;
            }

            $data = $form->getData();
            // Validate the data against its own constraints
$baseMessageContext->buildViolation($constraint->message)->addViolation();
        $baseViolations = $baseMessageContext->getViolations();
        $messages = [(string) $baseViolations->get(\count($baseViolations) - 1)->getMessage()];

        foreach ($constraint->constraints as $key => $item) {
            if (!\in_array($this->context->getGroup()$item->groups, true)) {
                continue;
            }

            $executionContext = clone $this->context;
            $executionContext->setNode($value$this->context->getObject()$this->context->getMetadata()$this->context->getPropertyPath());
            $violations = $validator->inContext($executionContext)->validate($value$item$this->context->getGroup())->getViolations();

            if (\count($this->context->getViolations()) === \count($violations)) {
                return;
            }

            if ($constraint->includeInternalMessages) {
                $message = ' ['.($key + 1).'] ';

                if ($item instanceof All || $item instanceof Collection) {
                    $message .= $constraint->messageCollection;
                } else {
                    
/** * @return void */
    public function validate(mixed $value, Constraint $constraint)
    {
        if (!$constraint instanceof Compound) {
            throw new UnexpectedTypeException($constraint, Compound::class);
        }

        $context = $this->context;

        $validator = $context->getValidator()->inContext($context);

        $validator->validate($value$constraint->constraints);
    }
}

        return $this->validator->validatePropertyValue($objectOrClass$propertyName$value$groups);
    }

    public function startContext(): ContextualValidatorInterface
    {
        return $this->validator->startContext();
    }

    public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
    {
        return $this->validator->inContext($context);
    }
}

        return $this->validator->validatePropertyValue($objectOrClass$propertyName$value$groups);
    }

    public function startContext(): ContextualValidatorInterface
    {
        return $this->validator->startContext();
    }

    public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
    {
        return $this->validator->inContext($context);
    }
}
$this->validator->initialize($this->context);
    }

    protected function setPropertyPath(string $propertyPath)
    {
        $this->propertyPath = $propertyPath;
        $this->context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
    }

    protected function expectNoValidate()
    {
        $validator = $this->context->getValidator()->inContext($this->context);
        $validator->expectNoValidate();
    }

    protected function expectValidateAt(int $i, string $propertyPath, mixed $value, string|GroupSequence|array|null $group)
    {
        $validator = $this->context->getValidator()->inContext($this->context);
        $validator->expectValidation($i$propertyPath$value$groupfunction D$passedConstraints) {
            $expectedConstraints = new LogicalOr();
            $expectedConstraints->setConstraints([new IsNull()new IsIdentical([])new IsInstanceOf(Valid::class)]);

            Assert::assertThat($passedConstraints$expectedConstraints);
        });
->setInvalidValue($value)
                ->setPlural((int) $constraint->min)
                ->setCode($exactlyOptionEnabled ? Count::NOT_EQUAL_COUNT_ERROR : Count::TOO_FEW_ERROR)
                ->addViolation();

            return;
        }

        if (null !== $constraint->divisibleBy) {
            $this->context
                ->getValidator()
                ->inContext($this->context)
                ->validate($count[
                    new DivisibleBy([
                        'value' => $constraint->divisibleBy,
                        'message' => $constraint->divisibleByMessage,
                    ]),
                ]$this->context->getGroup());
        }
    }
}

        if (!$constraint instanceof When) {
            throw new UnexpectedTypeException($constraint, When::class);
        }

        $context = $this->context;
        $variables = $constraint->values;
        $variables['value'] = $value;
        $variables['this'] = $context->getObject();

        if ($this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
            $context->getValidator()->inContext($context)
                ->validate($value$constraint->constraints);
        }
    }

    private function getExpressionLanguage(): ExpressionLanguage
    {
        if (!class_exists(ExpressionLanguage::class)) {
            throw new LogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" validator. Try running "composer require symfony/expression-language".', __CLASS__));
        }

        return $this->expressionLanguage ??= new ExpressionLanguage();
    }
$baseMessageContext->buildViolation($constraint->message)->addViolation();
        $baseViolations = $baseMessageContext->getViolations();
        $messages = [(string) $baseViolations->get(\count($baseViolations) - 1)->getMessage()];

        foreach ($constraint->constraints as $key => $item) {
            if (!\in_array($this->context->getGroup()$item->groups, true)) {
                continue;
            }

            $executionContext = clone $this->context;
            $executionContext->setNode($value$this->context->getObject()$this->context->getMetadata()$this->context->getPropertyPath());
            $violations = $validator->inContext($executionContext)->validate($value$item$this->context->getGroup())->getViolations();

            if (\count($this->context->getViolations()) === \count($violations)) {
                return;
            }

            if ($constraint->includeInternalMessages) {
                $message = ' ['.($key + 1).'] ';

                if ($item instanceof All || $item instanceof Collection) {
                    $message .= $constraint->messageCollection;
                } else {
                    
->setInvalidValue($value)
                ->setPlural((int) $constraint->min)
                ->setCode($exactlyOptionEnabled ? Count::NOT_EQUAL_COUNT_ERROR : Count::TOO_FEW_ERROR)
                ->addViolation();

            return;
        }

        if (null !== $constraint->divisibleBy) {
            $this->context
                ->getValidator()
                ->inContext($this->context)
                ->validate($count[
                    new DivisibleBy([
                        'value' => $constraint->divisibleBy,
                        'message' => $constraint->divisibleByMessage,
                    ]),
                ]$this->context->getGroup());
        }
    }
}
// If un-wrapped data has been passed, fetch the typed data object first.     if (!$data instanceof TypedDataInterface) {
      $data = $this->getTypedData();
    }
    if (!$data instanceof ComplexDataInterface) {
      throw new UnexpectedTypeException($data, 'ComplexData');
    }

    foreach ($constraint->properties as $name => $constraints) {
      $this->context->getValidator()
        ->inContext($this->context)
        // Specifically pass along FALSE as $root_call, as we validate the data         // as part of the typed data tree.         ->validate($data->get($name)$constraints, NULL, FALSE);
    }
  }

}
/** * @return void */
    public function validate(mixed $value, Constraint $constraint)
    {
        if (!$constraint instanceof Sequentially) {
            throw new UnexpectedTypeException($constraint, Sequentially::class);
        }

        $context = $this->context;

        $validator = $context->getValidator()->inContext($context);

        $originalCount = $validator->getViolations()->count();

        foreach ($constraint->constraints as $c) {
            if ($originalCount !== $validator->validate($value$c)->getViolations()->count()) {
                break;
            }
        }
    }
}
Home | Imprint | This part of the site doesn't use cookies.