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

Skip to content

Label translation parameters should not inherit from a form to its children #51520

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
stof opened this issue Aug 30, 2023 · 4 comments
Closed

Comments

@stof
Copy link
Member

stof commented Aug 30, 2023

Symfony version(s) affected

6.3

Description

Currently, \Symfony\Component\Form\Extension\Core\Type\BaseType::buildView merges the label_translation_parameters and attr_translation_parameters from the parent with the one.

This was unnoticed until now because the translation parameters defined explicitly still win and the translator component does not report anything in case extra parameters (not used in the translations) are provided.
However, when using a TranslatableMessage, the Twig extension complains if you pass a non-empty array of parameters alongside that value object as those parameters would be totally ignored (as the TranslatableMessage encapsulates its own parameters).
Even defining the option explicitly does not fix this as a merge is done.

A case where this can happen is when using an expanded choice type, for which your choices use TranslatableMessage and you use label_translation_parameters for the choice field itself (but actually, it can happen any time you use label_translation_parameters on a compound field).

How to reproduce

// in a form type

$excludeFolderCount = 12; // Actually coming from an option

$builder
    ->add('folder_delete_choice', ChoiceType::class, [
        'label' => 'folder.label.delete_choice',
        'label_translation_parameters' => [
            '%count%' => $excludeFolderCount,
        ],
        'data' => FolderDeleteAction::MOVE_CONTENT,
        'expanded' => true,
        'choices' => FolderDeleteAction::cases(),
        'choice_value' => 'value',
        'choice_label' => fn (FolderDeleteAction $action) => $action->getLabel(),
    ]);
use Symfony\Component\Translation\TranslatableMessage;

enum FolderDeleteAction: string
{
    case DELETE_CONTENT = 'delete_content';
    case MOVE_CONTENT = 'move_content';

    public function getLabel(): TranslatableMessage
    {
        return match ($this) {
            self::DELETE_CONTENT => new TranslatableMessage('folder.delete.choice.delete_content'),
            self::MOVE_CONTENT => new TranslatableMessage('folder.delete.choice.move_content'),
        };
    }
}

Possible Solution

Remove the inheritance of those parameters.

It might require going through a deprecation phase, requiring an opt-in (which would report a deprecation only when the parent parameters are not empty, to avoid requiring a useless opt-in for all cases where there is nothing inherited, i.e. in 99% of cases)

Additional Context

No response

@stof
Copy link
Member Author

stof commented Aug 30, 2023

Looking at the PR implementing it (#28635), this behavior has never been discussed in it (but it has been documented)

@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

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants