getViewData example

/** * @requires extension intl */
    public function testArabicLocale()
    {
        \Locale::setDefault('ar');

        $form = $this->factory->create(static::TESTED_TYPE);
        $form->submit('123456');

        $this->assertSame(123456, $form->getData());
        $this->assertSame('123456', $form->getViewData());
    }

    /** * @requires extension intl */
    public function testArabicLocaleNonHtml5()
    {
        \Locale::setDefault('ar');

        $form = $this->factory->create(static::TESTED_TYPE, null, ['grouping' => true]);
        $form->submit('123456');

        


    public function testSubmitNull($expected = null, $norm = null, $view = null)
    {
        $form = $this->factory->create($this->getTestedType(), null, [
            'widget' => 'choice',
        ]);
        $form->submit(null);

        $this->assertSame(['year' => null, 'week' => null]$form->getData());
        $this->assertSame(['year' => null, 'week' => null]$form->getNormData());
        $this->assertSame(['year' => null, 'week' => null]$form->getViewData());
    }

    public function testSubmitFromChoiceEmpty()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'widget' => 'choice',
            'required' => false,
        ]);

        $form->submit([
            'year' => '',
            
public function testSubmitSingleNonExpanded(string $class, string $submittedData, \UnitEnum $expectedData)
    {
        $form = $this->factory->create($this->getTestedType(), null, [
            'multiple' => false,
            'expanded' => false,
            'class' => $class,
        ]);

        $form->submit($submittedData);

        $this->assertEquals($expectedData$form->getData());
        $this->assertEquals($submittedData$form->getViewData());
        $this->assertTrue($form->isSynchronized());
    }

    /** * @dataProvider provideSingleSubmitData */
    public function testSubmitSingleExpanded(string $class, string $submittedData, \UnitEnum $expectedData)
    {
        $form = $this->factory->create($this->getTestedType(), null, [
            'multiple' => false,
            'expanded' => true,
            
        $builder->setData($options['data'] ?? false);
        $builder->addViewTransformer(new BooleanToStringTransformer($options['value']$options['false_values']));
    }

    /** * @return void */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars = array_replace($view->vars, [
            'value' => $options['value'],
            'checked' => null !== $form->getViewData(),
        ]);
    }

    /** * @return void */
    public function configureOptions(OptionsResolver $resolver)
    {
        $emptyData = static fn (FormInterface $form$viewData) => $viewData;

        $resolver->setDefaults([
            

            }

            // Mark the form with an error if it is not synchronized BUT all             // 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()
                    ))
'widget' => 'single_text',
            'input' => 'datetime',
        ]);

        $outputTime = new \DateTime('2010-06-02 03:04:00 Pacific/Tahiti');

        $form->submit('2010-06-02T03:04:00');

        $outputTime->setTimezone(new \DateTimeZone('America/New_York'));

        $this->assertEquals($outputTime$form->getData());
        $this->assertEquals('2010-06-02T03:04', $form->getViewData());
    }

    public function testSubmitDifferentTimezonesDateTimeImmutable()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'model_timezone' => 'America/New_York',
            'view_timezone' => 'Pacific/Tahiti',
            'widget' => 'single_text',
            'input' => 'datetime_immutable',
        ]);

        
$form = $this->getBuilder()
            ->setCompound(true)
            ->setDataMapper(new DataMapper())
            ->getForm();

        $child = $this->getBuilder()->getForm();

        $form->add($child);

        $this->assertNull($form->getData());
        $this->assertNull($form->getNormData());
        $this->assertNull($form->getViewData());
    }

    public function testAddDoesNotMapViewDataToFormIfInheritData()
    {
        $form = $this->getBuilder()
            ->setCompound(true)
            ->setDataMapper(new DataMapper())
            ->getForm();

        $child = $this->getBuilder()
            ->setInheritData(true)
            
$this->assertSame('Second label', $form['second']->getConfig()->getOption('label'));
        $this->assertTrue($form['first']->isRequired());
        $this->assertTrue($form['second']->isRequired());
    }

    public function testSubmitUnequal()
    {
        $input = ['first' => 'foo', 'second' => 'bar'];

        $this->form->submit($input);

        $this->assertSame('foo', $this->form['first']->getViewData());
        $this->assertSame('bar', $this->form['second']->getViewData());
        $this->assertFalse($this->form->isSynchronized());
        $this->assertSame($input$this->form->getViewData());
        $this->assertNull($this->form->getData());
    }

    public function testSubmitEqual()
    {
        $input = ['first' => 'foo', 'second' => 'foo'];

        $this->form->submit($input);

        
'foo' => 'bar',
        ]);

        $config = new FormConfigBuilder('name', null, new EventDispatcher());
        $config->addViewTransformer($view);
        $config->addModelTransformer($model);
        $config->setData('default');
        $form = new Form($config);

        $this->assertSame('default', $form->getData());
        $this->assertSame('foo', $form->getNormData());
        $this->assertSame('bar', $form->getViewData());
    }

    public function testDataTransformationFailure()
    {
        $this->expectException(TransformationFailedException::class);
        $this->expectExceptionMessage('Unable to transform data for property path "name": No mapping for value "arg"');
        $model = new FixedDataTransformer([
            'default' => 'foo',
        ]);
        $view = new FixedDataTransformer([
            'foo' => 'bar',
        ]);
$this->assertEquals(0.0124, $form->getData());
    }

    public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedData = 0.1)
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'empty_data' => $emptyData,
            'rounding_mode' => \NumberFormatter::ROUND_UP,
        ]);
        $form->submit(null);

        $this->assertSame($emptyData$form->getViewData());
        $this->assertSame($expectedData$form->getNormData());
        $this->assertSame($expectedData$form->getData());
    }

    public function testHtml5EnablesSpecificFormatting()
    {
        \Locale::setDefault('de_CH');

        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'html5' => true,
            'rounding_mode' => \NumberFormatter::ROUND_UP,
            
public function testSubmitNullWhenMultiple()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'multiple' => true,
        ]);
        // submitted data when an input file is uploaded without choosing any file         $form->submit([null]);

        $this->assertSame([]$form->getData());
        $this->assertSame([]$form->getNormData());
        $this->assertSame([]$form->getViewData());
    }

    /** * @dataProvider requestHandlerProvider */
    public function testSubmittedFilePathsAreDropped(RequestHandlerInterface $requestHandler)
    {
        $form = $this->factory->createBuilder(static::TESTED_TYPE)->setRequestHandler($requestHandler)->getForm();
        $form->submit('file:///etc/passwd');

        $this->assertNull($form->getData());
        
            if (!isset($view->vars['attr']['readonly']) && isset($view->parent->vars['attr']['readonly']) && false !== $view->parent->vars['attr']['readonly']) {
                $view->vars['attr']['readonly'] = true;
            }

            $helpTranslationParameters = array_merge($view->parent->vars['help_translation_parameters']$helpTranslationParameters);
        }

        $formConfig = $form->getConfig();
        $view->vars = array_replace($view->vars, [
            'errors' => $form->getErrors(),
            'valid' => $form->isSubmitted() ? $form->isValid() : true,
            'value' => $form->getViewData(),
            'data' => $form->getNormData(),
            'required' => $form->isRequired(),
            'label_attr' => $options['label_attr'],
            'help' => $options['help'],
            'help_attr' => $options['help_attr'],
            'help_html' => $options['help_html'],
            'help_translation_parameters' => $helpTranslationParameters,
            'compound' => $formConfig->getCompound(),
            'method' => $formConfig->getMethod(),
            'action' => $formConfig->getAction(),
            'submitted' => $form->isSubmitted(),
        ]);


        if ($options['multiple']) {
            $messageTemplate = $options['invalid_message'] ?? 'The value {{ value }} is not valid.';
            $translator = $this->translator;

            $builder->addEventListener(FormEvents::POST_SUBMIT, static function DFormEvent $event) use (&$unknownValues$messageTemplate$translator) {
                // Throw exception if unknown values were submitted                 if (\count($unknownValues) > 0) {
                    $form = $event->getForm();

                    $clientDataAsString = \is_scalar($form->getViewData()) ? (string) $form->getViewData() : (\is_array($form->getViewData()) ? implode('", "', array_keys($unknownValues)) : \gettype($form->getViewData()));

                    if ($translator) {
                        $message = $translator->trans($messageTemplate['{{ value }}' => $clientDataAsString], 'validators');
                    } else {
                        $message = strtr($messageTemplate['{{ value }}' => $clientDataAsString]);
                    }

                    $form->addError(new FormError($message$messageTemplate['{{ value }}' => $clientDataAsString], null, new TransformationFailedException(sprintf('The choices "%s" do not exist in the choice list.', $clientDataAsString))));
                }
            });

            

        $form = $this->factory->createBuilder(static::TESTED_TYPE)
            ->addViewTransformer(new FixedDataTransformer([
                '' => '',
                'reverse[a]' => 'a',
            ]))
            ->setCompound(false)
            ->getForm();

        $form->submit(' a ');

        $this->assertEquals('a', $form->getViewData());
        $this->assertEquals('reverse[a]', $form->getData());
    }

    public function testSubmittedDataIsNotTrimmedBeforeTransformingIfNoTrimming()
    {
        $form = $this->factory->createBuilder(static::TESTED_TYPE, null, ['trim' => false])
            ->addViewTransformer(new FixedDataTransformer([
                '' => '',
                'reverse[ a ]' => ' a ',
            ]))
            ->setCompound(false)
            
'allow_delete' => true,
            'delete_empty' => true,
        ]);

        $form->submit(['a', 'x', '']);

        $this->assertSame(['a']$form->getData());
        $this->assertCount(2, $form);
        $this->assertTrue($form->has('1'));
        $this->assertFalse($form[1]->isValid());
        $this->assertNull($form[1]->getData());
        $this->assertSame('x', $form[1]->getViewData());
    }

    public function testNotResizedIfSubmittedWithExtraData()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'entry_type' => TextTypeTest::TESTED_TYPE,
        ]);
        $form->setData(['foo@bar.com']);
        $form->submit(['foo@foo.com', 'bar@bar.com']);

        $this->assertTrue($form->has('0'));
        
Home | Imprint | This part of the site doesn't use cookies.