【发布时间】:2017-03-08 10:50:33
【问题描述】:
我有一个带有 EntityType 字段的表单,用于检索酒店列表。
我想在该列表中添加一个选项“全选”以直接获取所有酒店的列表。
我创建了一个 EntityTypeExtension(我现在更喜欢自定义扩展类型),其中包含以下内容:
public function finishView(FormView $view, FormInterface $form, array $options)
{
$list = $this->em->getRepository($options['class'])->findAll();
$new_choice = new ChoiceView($list, 'all', 'Select all'); // <- new option
$view->vars['choices'][] = $new_choice;//<- adding the new option
}
这可行,但如果我选择“全选”,我会得到以下堆栈:
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[hotel] = all
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "[hotel]": The choice "all" does not exist or is not unique
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
The choice "all" does not exist or is not unique
我假设 DataTransformer 无法识别“全部”选项。
你有什么想法吗?我应该重载 EntityType DataTransformer 吗?
【问题讨论】:
标签: php forms symfony symfony-forms