LazyChoiceListを拡張し、新しいFormTypeを実装する必要があるかもしれないと思っています。これまでのところ、次のようになっています。
/**
* 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でこの種のアプローチを試しましたが、データ(null値)にアクセスできないか、choice_listを更新しても、すでに確認されているため、効果がありません。処理されました。