getTransformationFailure example

public function testArrayTransformationFailureOnSubmit()
    {
        $this->form->add($this->getBuilder('foo')->setCompound(false)->getForm());
        $this->form->add($this->getBuilder('bar', null, ['multiple' => false])->setCompound(false)->getForm());

        $this->form->submit([
            'foo' => ['foo'],
            'bar' => ['bar'],
        ]);

        $this->assertNull($this->form->get('foo')->getData());
        $this->assertSame('Submitted data was expected to be text or number, array given.', $this->form->get('foo')->getTransformationFailure()->getMessage());

        $this->assertNull($this->form->get('bar')->getData());
        $this->assertSame('Submitted data was expected to be text or number, array given.', $this->form->get('bar')->getTransformationFailure()->getMessage());
    }

    public function testFileUpload()
    {
        $reqHandler = new HttpFoundationRequestHandler();
        $this->form->add($this->getBuilder('foo')->setRequestHandler($reqHandler)->getForm());
        $this->form->add($this->getBuilder('bar')->setRequestHandler($reqHandler)->getForm());

        
            // of its children are synchronized. If any child is not             // synchronized, an error is displayed there already and showing             // a second error in its parent form is pointless, or worse, may             // lead to duplicate errors if error bubbling is enabled on the             // child.             // See also https://github.com/symfony/symfony/issues/4359             if ($childrenSynchronized) {
                $clientDataAsString = \is_scalar($form->getViewData())
                    ? (string) $form->getViewData()
                    : get_debug_type($form->getViewData());

                $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)
                    
FormEvents::POST_SUBMIT => ['convertTransformationFailureToFormError', -1024],
        ];
    }

    /** * @return void */
    public function convertTransformationFailureToFormError(FormEvent $event)
    {
        $form = $event->getForm();

        if (null === $form->getTransformationFailure() || !$form->isValid()) {
            return;
        }

        foreach ($form as $child) {
            if (!$child->isSynchronized()) {
                return;
            }
        }

        $clientDataAsString = \is_scalar($form->getViewData()) ? (string) $form->getViewData() : get_debug_type($form->getViewData());
        $messageTemplate = $form->getConfig()->getOption('invalid_message', 'The value {{ value }} is not valid.');
        

    public function testSubmitInvalidNestedValue($multiple$expanded$submissionData)
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'choices' => $this->choices,
            'multiple' => $multiple,
            'expanded' => $expanded,
        ]);

        $form->submit($submissionData);
        $this->assertFalse($form->isSynchronized());
        $this->assertInstanceOf(TransformationFailedException::class$form->getTransformationFailure());
        if (!$multiple && !$expanded) {
            $this->assertEquals('Submitted data was expected to be text or number, array given.', $form->getTransformationFailure()->getMessage());
        } else {
            $this->assertEquals('All choices submitted must be NULL, strings or ints.', $form->getTransformationFailure()->getMessage());
        }
    }

    public static function invalidNestedValueTestMatrix()
    {
        return [
            'non-multiple, non-expanded' => [false, false, [[]]],
            
->add('amount', TextType::class)
            ->add('currency', CurrencyType::class)
        ;
        $builder->setDataMapper(new MoneyDataMapper());
        $form = $builder->getForm();

        $form->submit(['amount' => 'invalid_amount', 'currency' => 'USD']);

        $this->assertFalse($form->isValid());
        $this->assertNull($form->getData());
        $this->assertCount(1, $form->getErrors());
        $this->assertSame('Expected numeric value', $form->getTransformationFailure()->getMessage());
        $error = $form->getErrors()[0];
        $this->assertSame('Money amount should be numeric. "invalid_amount" is invalid.', $error->getMessage());
    }

    // https://github.com/symfony/symfony/issues/6862     public function testPassZeroLabelToView()
    {
        $view = $this->factory->create(static::TESTED_TYPE, null, [
                'label' => '0',
            ])
            ->createView();

        
$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
Home | Imprint | This part of the site doesn't use cookies.