我想我可能需要延长LazyChoiceList和实施新的FormType,所以到目前为止,我有:如何在Symfony 2.1的FormEvent中更新ChoiceType的值?
/**
* A choice list for sorting choices.
*/
class SortChoiceList extends LazyChoiceList
{
private $choices = array();
public function getChoices() {
return $this->choices;
}
public function setChoices(array $choices) {
$this->choices = $choices;
return $this;
}
protected function loadChoiceList() {
return new SimpleChoiceList($this->choices);
}
}
和
/**
* @FormType
*/
class SortChoice extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->getParent()->addEventListener(FormEvents::PRE_SET_DATA, function($event) use ($options) {
$options = (object) $options;
$list = $options->choice_list;
$data = $event->getData();
if ($data->getLocation() && $data->getDistance()) {
$list->setChoices(array(
'' => 'Distance',
'highest' => 'Highest rated',
'lowest' => 'Lowest rated'
));
} else {
$list->setChoices(array(
'' => 'Highest rated',
'lowest' => 'Lowest rated'
));
}
});
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'sort_choice';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'choice_list' => new SortChoiceList
));
}
}
我试过这种方法对所有的可用的FormEvent的,但我没有访问数据(空值)或更新choice_list没有效果,据我所知,因为它已经被处理。
我已经看到,是的,事实上我在我的回答中引用了它,它在我的示例中有特征,但在我的情况下,对于这种形式,空值永远不会传递,所以为了简单。 – Steve 2012-08-13 10:10:34