如何在 Symfony 2.1 中使用 ChoiceList? [英] How to use ChoiceList in Symfony 2.1?
问题描述
我有一个包含美国各州列表的文件.
阿拉巴马州
阿拉斯加
等等..
在 symfony 2.0 中,我使用 ChoiceListInterface.php 在我的表单中使用它.我只是写了这个:
但是现在在 ChoiceListInterface 中还有 7 个其他函数要实现:
公共函数getValues();公共函数 getPreferredViews();公共函数 getRemainingViews();公共函数 getValuesForChoices(array $choices);公共函数 getIndicesForChoices(array $choices);公共函数 getIndicesForValues(array $values);
我已阅读文档http:///api.symfony.com/2.1/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.html 但就我而言,我觉得不清楚,我真的不明白如何实现它们.>
有人可以帮忙吗?非常感谢
你可以扩展 LazyChoiceList 并实现 loadChoiceList() 方法,您可以在其中返回一个新的 ChoiceList 对象填充了从文件中读取的值.
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
You can extend LazyChoiceList and implement loadChoiceList() method where you can return an new ChoiceList object filled with the values read from the file.
这篇关于如何在 Symfony 2.1 中使用 ChoiceList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!