我有一个包含美国各州列表的文件。
阿拉巴马州
阿拉斯加
等等..
在 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/