How to use ChoiceList in Symfony 2.1?

别等时光非礼了梦想. 提交于 2019-12-05 01:53:20

问题


I have a file containing a list of the US states.
Alabama
Alaska
etc ..

In symfony 2.0 I used ChoiceListInterface.php to use it in my form. I simply wrote this :

<?php

namespace MyBundle\Form;

use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;

class StateChoiceList implements ChoiceListInterface
{
    public function getChoices()
    {
        $lines = file('listes/us_states.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        // fill the array
        $arr = array();
        foreach ($lines as $line) {
            $arr[$line] = $line;
        }
        return $arr;

    }
}

But now there is 7 other functions to implement in ChoiceListInterface :

public function getValues();
public function getPreferredViews();
public function getRemainingViews();
public function getValuesForChoices(array $choices);
public function getIndicesForChoices(array $choices);
public function getIndicesForValues(array $values);

I have read the documentation http://api.symfony.com/2.1/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.html but in my case I find it unclear and I really don't understand how to implement them.

Someone could help ? Thanks a lot


回答1:


You can extend LazyChoiceList and implement loadChoiceList() method where you can return an new ChoiceList object filled with the values read from the file.



来源:https://stackoverflow.com/questions/13257787/how-to-use-choicelist-in-symfony-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!