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

Skip to content

[2.1] [Validator] [Translator] The Choice validator fails when translation is enabled #4900

@hhamon

Description

@hhamon

The Choice constraint fails for multiple choices with the translator enabled.

My application is configured with the French locale by default and the translator is enabled in my app/config/config.yml file.

I have a simple form that combines two fields: email and interests:

<?php
// ...
class DefaultController extends Controller
{
    public function indexAction()
    {
        $profile = new Profile();

        $form = $this
            ->createFormBuilder($profile)
            ->add('email', 'email')
            ->add('interests', 'choice', array(
                'multiple' => true,
                'choices' => array(
                    'music' => 'Music',
                    'computer' => 'Computers',
                    'movies' => 'Movies',
                    'games' => 'Games',
                    'cooking' => 'Cooking',
                    'travels' => 'Travels',
                )
            ))
            ->getForm()
        ;

        $request = $this->getRequest();
        if ($request->isMethod('post')) {
            $form->bindRequest($request);
            if ($form->isValid()) {
                var_dump($profile);
                die;
            }
        }

        return array('form' => $form->createView());
    }
}

The Profile class is as follow:

    <?php

namespace Sensio\DemoBundle\Domain;

use Symfony\Component\Validator\Constraints as Assert;

class Profile
{
    /**
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    public $email;

    /**
     * @Assert\NotBlank()
     * @Assert\Choice(
     *   choices = {
     *     "music",
     *     "computer",
     *     "movies",
     *     "games",
     *     "cooking",
     *     "travels"
     *  },
     *  min = 2,
     *  multiple = true
     * )
     */
    public $interests = array();
}

The user is required to tick at least two interests in the list.

When the translator is not activated, the template is correctly rendered with the following error message:

You must select at least 2 choices.

If I activate the translator with the french locale, I get the two following combined exceptions:

[Exception 2/2]
An exception has been thrown during the rendering of a template ("Unable to choose a translation for "Vous devez sélectionner au moins {{ limit }} choix." with locale "fr".") in kernel.root_dir/Resources/views/base.html.twig at line 11. 

[Exception 1/2]
InvalidArgumentException: Unable to choose a translation for "Vous devez sélectionner au moins {{ limit }} choix." with locale "fr"

It seems the translator is not able to choose for the right translation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions