在预提交事件期间删​​除 ChoiceType 转换器

Removing ChoiceType transformers during Pre Submit events

我有一个表单,里面有一个集合子表单。在子表单中,属性 "resourceId" 有一个 choiceType 字段。它的数据由 ajax 填充,带有 Select2 js 插件(因为它的数据取决于另一个选择,其中您 select 资源类型)。

在我已经在 resourceId 选择中有一个值的特定情况下,我无法验证我的字段:

transformationFailure: TransformationFailedException {#4328 ▼
  #message: "Unable to reverse value for property path "resourceId": The choice "bd922d35fb828da6e39edf3c7927511c9a6be025" does not exist or is not unique"

这是因为我必须通过 javascript 添加字段的默认值(感谢 Select2)。

我需要取消对该字段的验证,但即使我在 BuildForm 方法中使用 ResetViewTransformers() 并通过在 PreSubmit 事件中重建该字段,它仍然无法验证。

TL;DR:如何在 PreSubmit 事件期间取消验证? (如果可能,只在我的领域)

添加一些 jquery 并将 .ignore class 添加到您的特定表单字段。如果您加载整个表单,我建议您单独加载它们,这样您就可以轻松添加它。

$("#myform").validate({
   ignore: ".ignore, :hidden"
})

隐藏也会确保你隐藏的任何字段,不会因为其他原因而受影响然后报错,参考:here

找到解决方案。我走错方向了。

我所要做的就是覆盖 ChoiceType class 并禁用 ViewTransformers,然后使用新的 class :

    class NonTransformedChoiceType extends ChoiceType
   {
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder->resetModelTransformers();
        $builder->resetViewTransformers();
    }
}