Skip to content
Advertisement

Symfony3: Correspondence of key value reversal in Choice Type

I’m updating Symfony.
When I upgraded to Symfony3, all the “State.” Names displayed on the form changed to numbers.
All state values are set in services.yml. By reversing the yml value, the display in the browser was restored, but when I tried to save the value selected in the pull-down in the form, the system works to save the number instead of “State.”.
Is there any good solution? Also, I don’t want to use “ `choices_as_value“` because it will not be usable in the future.

Code
Type

        $builder->add("prefId", ChoiceType::class, array(
            "required" => true,
            "choices" => Parameters::getParameter("state"),
        ));

services.yml

    state:
      //Symfoyn 2
        1: "Alabama"
      //Symfony 3
        "Alabama": 1

Version
Symfony3.0.9 PHP 5.6

Advertisement

Answer

I solved the problem by changing array_keys in Parameter.php to array_reverse. Thanks to Cerad.

    //Parameter.php
   /**
     * 
     *
     * @return array 
     */
    public static function getState()
    {
        return self::$container->getParameter("state");
    }

    /**
     * 
     *
     * @return array 
     */
    public static function getStateKeys()
    {
        //return array_keys(self::getState());
        return array_reverse(self::getState());
    }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement