Choics labels are not translated when serializing ChoiceType
wewelll opened this issue · comments
Hi,
When we have a choiceType, the attribute 'choices' is an array with key
and value
.
When the form is serialized, each key
goes in the enum_titles
but they are not translated.
I think that the expected behaviour is to have these keys translated, as this is the default behaviour in the createView
method of Symfony.
Do you know if this would be possible ?
Thanks for reporting this, @samuelbriole
Version: "limenius/liform-bundle": "^0.12.0"
Symfony: 4.0
If you extend a Form or have a single Field before, then set a translation domain it seems like that domain is applied permanently. I could not pin down the problem exactly but you can easily reproduce this problem by extending thd FOSUserBundle
registration form calling the parent buildForm
in an overriden buildForm
of the newly created Form class.
Then add a choice field with translations from your own app and set the choice_translation_domain
to your domain. The ChoiceTransformer ignores the Domain of the Choice widget and uses the FOSUserBundle translator domain instead of the one I defined in the form.
I would provide a fix, but I had trouble finding the code segment where the translator is actually given.
kind regards
Proposal: #25
Tests are failing, I have not yet looked into why.
Example:
<?php
/**
* Created by PhpStorm.
* User:
* Date: 11.02.18
* Time: 18:23
*/
namespace App\Form;
use App\Entity\User;
use FOS\UserBundle\Form\Type\RegistrationFormType as FOSUserBundleRegistrationFormType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class RegistrationFormType extends FOSUserBundleRegistrationFormType
{
protected $class;
public function __construct(string $class = User::class)
{
parent::__construct($class);
$this->class = $class;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('title', ChoiceType::class, [
'choice_translation_domain' => 'form',
//'translation_domain' => 'form',
'choices' => [
'title_female' => 'title_female_value',
'title_male' => 'title_male_value',
],
])
->add('firstname', TextType::class)
->add('lastname', TextType::class);
$builder->remove('plainPassword');
$builder->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'options' => [
'translation_domain' => 'FOSUserBundle',
'attr' => [
'autocomplete' => 'new-password',
],
],
'first_options' => ['label' => 'form.password'],
'second_options' => ['label' => 'form.password_confirmation'],
'invalid_message' => 'fos_user.password.mismatch',
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => $this->class,
'csrf_protection' => false,
]);
}
}
Long overdue, merged #44