setCause example

$this->assertTrue($form->isSubmitted());
        $this->assertFalse($form->isSynchronized());
        $this->expectNoValidate();

        $this->validator->validate($formnew Form());

        $this->buildViolation('invalid_message_key')
            ->setParameter('{{ value }}', 'foo')
            ->setParameter('{{ foo }}', 'bar')
            ->setInvalidValue('foo')
            ->setCode(Form::NOT_SYNCHRONIZED_ERROR)
            ->setCause($form->getTransformationFailure())
            ->assertRaised();
    }

    public function testAddInvalidErrorEvenIfNoValidationGroups()
    {
        $object = new \stdClass();

        $form = $this->getBuilder('name', '\stdClass', [
                'invalid_message' => 'invalid_message_key',
                // Invalid message parameters must be supported, because the                 // invalid message can be a translation key
$this->validator->validate($entity1$constraint);

        $this->assertNoViolation();

        $this->validator->validate($entity2$constraint);

        $this->buildViolation('myMessage')
            ->atPath('property.path.name')
            ->setParameter('{{ value }}', '"Foo"')
            ->setInvalidValue($entity2)
            ->setCause([$entity1])
            ->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
            ->assertRaised();
    }

    public static function provideUniquenessConstraints(): iterable
    {
        yield 'Doctrine style' => [new UniqueEntity([
            'message' => 'myMessage',
            'fields' => ['name'],
            'em' => self::EM_NAME,
        ])];

        
return;
        }

        $errorPath = $constraint->errorPath ?? $fields[0];
        $invalidValue = $criteria[$errorPath] ?? $criteria[$fields[0]];

        $this->context->buildViolation($constraint->message)
            ->atPath($errorPath)
            ->setParameter('{{ value }}', $this->formatWithIdentifiers($em$class$invalidValue))
            ->setInvalidValue($invalidValue)
            ->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
            ->setCause($result)
            ->addViolation();
    }

    private function ignoreNullForField(UniqueEntity $constraint, string $fieldName): bool
    {
        if (\is_bool($constraint->ignoreNull)) {
            return $constraint->ignoreNull;
        }

        return \in_array($fieldName(array) $constraint->ignoreNull, true);
    }

    
$failure = $form->getTransformationFailure();

                $this->context->setConstraint($formConstraint);
                $this->context->buildViolation($failure->getInvalidMessage() ?? $config->getOption('invalid_message'))
                    ->setParameters(array_replace(
                        ['{{ value }}' => $clientDataAsString],
                        $config->getOption('invalid_message_parameters'),
                        $failure->getInvalidMessageParameters()
                    ))
                    ->setInvalidValue($form->getViewData())
                    ->setCode(Form::NOT_SYNCHRONIZED_ERROR)
                    ->setCause($failure)
                    ->addViolation();
            }
        }

        // Mark the form with an error if it contains extra fields         if (!$config->getOption('allow_extra_fields') && \count($form->getExtraData()) > 0) {
            $this->context->setConstraint($formConstraint);
            $this->context->buildViolation($config->getOption('extra_fields_message', ''))
                ->setParameter('{{ extra_fields }}', '"'.implode('", "', array_keys($form->getExtraData())).'"')
                ->setPlural(\count($form->getExtraData()))
                ->setInvalidValue($form->getExtraData())
                
->setCode('5')
            ->addViolation();

        $this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, []$this->root, 'data', 'foo', null, '5', new Valid()));
    }

    public function testCauseCanBeSet()
    {
        $cause = new \LogicException();

        $this->builder
            ->setCause($cause)
            ->addViolation();

        $this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, []$this->root, 'data', 'foo', null, null, new Valid()$cause));
    }

    public function testTranslationDomainFalse()
    {
        $translator = $this->createMock(TranslatorInterface::class);
        $translator->expects(self::once())->method('trans')->willReturn('');

        $builder = new ConstraintViolationBuilder($this->violations, new Valid()$this->messageTemplate, []$this->root, 'data', 'foo', $translator);
        
Home | Imprint | This part of the site doesn't use cookies.