symfony - 如何在 Symfony 2.1 中使用 ChoiceList?

标签 symfony symfony-2.1

我有一个包含美国各州列表的文件。
阿拉巴马州
阿拉斯加
等等..

在 symfony 2.0 中,我使用 ChoiceListInterface.php 在我的表单中使用它。我只是写了这个:

<?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;

    }
}

但是现在 ChoiceListInterface 中还有 7 个其他函数需要实现:

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

我已阅读文档 http://api.symfony.com/2.1/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.html但就我而言,我发现它不清楚,我真的不明白如何实现它们。

有人可以帮忙吗?非常感谢

最佳答案

您可以扩展LazyChoiceList并实现loadChoiceList()方法,您可以返回新的 ChoiceList对象填充从文件读取的值。

关于symfony - 如何在 Symfony 2.1 中使用 ChoiceList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13257787/

相关文章:

php - 通过刷新时的外部实体异常来 Doctrine OneToOne 身份

php - 具有相同字段名称的 Doctrine 查询生成器

symfony - 如何在 Symfony 中自定义 DataTransformer 错误消息?

symfony - composer.json 无法解析可安装的软件包集

symfony - 检查多个用户对一个对象的 ACL 权限

forms - 如何在 Twig 模板中隐藏表单字段?

php - Symfony Doctrine EntityManager 没有正确刷新

php - 如何让 Composer/PSR-4 接受子文件夹中的第二个命名空间?

symfony - 使用 FOSRestBundle 和 Symfony2 覆盖路由中的默认值 ".{_format}"

symfony - Doctrine Timestampable 扩展不持久