Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Closure for empty_data with empty string converted to null #25979

@soullivaneuh

Description

@soullivaneuh
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version v3.4.4

With this php sample:

declare(strict_types=1);

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\ResolvedFormTypeFactory;

require_once __DIR__.'/vendor/autoload.php';

class ThingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', null, [
                'empty_data' => '',
            ])
            ->add('closure', null, [
                'empty_data' => function (FormInterface $form) {
                    return '';
                },
            ])
        ;
    }
}

$formFactory = new FormFactory(new FormRegistry([], new ResolvedFormTypeFactory()));
$form = $formFactory->create(ThingType::class);
$form->submit([]);
dump($form->getData());

You have:

sullivan@c8dc0d9ca068:/code$ php test.php 
array:2 [
  "name" => ""
  "closure" => null
]

Both should be empty strings.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions