Skip to content

Form field 'placeholder' attribute domain issue #96

@PomepuyN

Description

@PomepuyN

When using a placeholder attr for a form field, the extractor doesn't respect the domain.
The translation is then in the wrong domain file and cannot be retrieved at layout.

Example

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name',
                TextType::class,
                array('required' => false,
                    'attr' => array(
                        'placeholder' => 'label.hint.name'
                    ),
                    "label" => "label.name",

                    'translation_domain' => 'contact'
                )
            )
    ...
    }

In this case, label.name is extracted in the contact.en.xlf file and label.hint.name in the messages.en.xlf file.
But when rendered, even if the hints are translated, it's not rendered because the translator wants it to be in the contact domain.

As a workaround I had to add:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /** @var Translator $translator */
        $translator = $options['translator'];


        //workaround for placeholders :(
        $translator->trans('label.hint.name', array(), 'contact');

        $builder
            ->add(...);
        
    }

It fixes the issue, but these entries are now duplicated (in messages.en.xlf and contact.en.xlf)

I also tried to directly set the placeholder with a result of $translator->trans(), but this way I get a Form placeholder is not a scalar string error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions