【问题标题】:Adding an option "Select all" to an EntityType Field returns the error "Unable to reverse value for property path"将选项“全选”添加到 EntityType 字段会返回错误“无法反转属性路径的值”
【发布时间】: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


    【解决方案1】:

    您需要在表单类型选项中添加multiple =&gt; true 以允许自动转换一组 id(假设您的实体有一组酒店),或者您需要实现自己的数据转换器(这非常简单)。

    在你的情况下,你需要一个视图转换器

    数据转换器文档:http://symfony.com/doc/current/form/data_transformers.html

    我认为 ChoiceView 构造函数的 value 参数(第二个参数)应该是 $list 中对象 id 的数组,因为 EntityType 期望选择的值是对象 id。

    【讨论】:

      猜你喜欢
      • 2021-06-21
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 2022-11-12
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多