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

Skip to content

RepeatedType does not respect the "options.empty_data" option #47013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ludekbenedik opened this issue Jul 21, 2022 · 13 comments
Closed

RepeatedType does not respect the "options.empty_data" option #47013

ludekbenedik opened this issue Jul 21, 2022 · 13 comments
Labels
Bug Form Good first issue Ideal for your first contribution! (some Symfony experience may be required) Status: Needs Review

Comments

@ludekbenedik
Copy link

ludekbenedik commented Jul 21, 2022

Symfony version(s) affected

5.4.10

Description

RepeatedType sets NULL even though the "empty_data" option is set to the empty string.

How to reproduce

AccountType.php

<?php declare(strict_types = 1);

namespace App\Form\Type;

use App\Form\Data\AccountData;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AccountType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('password', RepeatedType::class, [
            'type' => PasswordType::class,
            'required' => false,
            'empty_data' => '', // Does not work
            'options' => [
                'empty_data' => '', // Does not work
            ],
        ]);

        // Hack: RepeatedType does not respect empty_data option
        //$builder->get('password')->addEventListener(FormEvents::SUBMIT, function (FormEvent $event): void {
        //    if (null === $event->getData()) {
        //        $event->setData('');
        //    }
        //});
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => AccountData::class,
        ]);
    }
}

AccountData.php

<?php declare(strict_types = 1);

namespace App\Form\Data;

class AccountData
{
    public string $password = '';
}

Possible Solution

No response

Additional Context

No response

@HeahDude
Copy link
Contributor

HeahDude commented Jul 21, 2022

Hello @ludekbenedik, it seems expected since the empty_data expects a value in the view format, in this case a repeated value like:

    $builder->add('password', RepeatedType::class, [
            'type' => PasswordType::class,
            'required' => false,
            'empty_data' => ['', ''], // should work
    ]);

@ludekbenedik
Copy link
Author

ludekbenedik commented Jul 25, 2022

Hello @HeahDude, unfortunately, it does not work.

@MatTheCat
Copy link
Contributor

I guess this comes from

Sadly I don’t think data transformers can access form options 😬

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

@ludekbenedik
Copy link
Author

Hi,
is there a possibility to set empty_data to RepeatedType?
Thanks

@carsonbot carsonbot removed the Stalled label Mar 16, 2023
@mmenozzi
Copy link
Contributor

This bug is still relevant even on Symfony 6.

@sovetski
Copy link

Same here

@scristian9
Copy link

Still persist 😵

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@yceruto yceruto added the Good first issue Ideal for your first contribution! (some Symfony experience may be required) label Jul 23, 2024
@yceruto
Copy link
Member

yceruto commented Jul 23, 2024

This can be solved by adding a custom view transformer before ValueToDuplicatesTransformer in RepeatedType, something like:

if ('' === $options['empty_data']) {
    $builder->addViewTransformer(new CallbackTransformer(
        fn (mixed $value) => $value,
        fn (mixed $value) => $value ?? '',
    ));
}

Good first issue for new contributors. PR is welcomed!

@carsonbot carsonbot removed the Stalled label Jul 23, 2024
@yceruto
Copy link
Member

yceruto commented Jul 23, 2024

By the way, I’m not sure this should be classified as a ‘bug’; it feels more like a missing feature to me. In that sense, it can be introduced in newest branch.

@xabbuh
Copy link
Member

xabbuh commented Aug 5, 2024

I looked at this issue again and I think the way we decide if data is empty inside the ValueToDuplicatesTransformer is not correct. Thus I propose to fix it there instead (see #57920).

nicolas-grekas added a commit that referenced this issue Aug 8, 2024
…rmer (xabbuh)

This PR was merged into the 5.4 branch.

Discussion
----------

[Form] Fix handling empty data in ValueToDuplicatesTransformer

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #47013
| License       | MIT

The transformer receives the data of child forms that have already been through the transformation schema. If no custom view transformer was used on that child form the empty would already have been changed to null. Thus, receiving an empty string here means that the child form explicitly asked for it and the value must not be exchanged with null.

Commits
-------

4ec6c80 fix handling empty data in ValueToDuplicatesTransformer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Form Good first issue Ideal for your first contribution! (some Symfony experience may be required) Status: Needs Review
Projects
None yet
Development

No branches or pull requests

10 participants