2017-01-02 66 views
0

我有一个带有收集子窗体的窗体。在子表单中,属性“resourceId”有一个choiceType字段。其数据由ajax和Select2 js插件填充(因为它的数据取决于另一种选择,您可以在其中选择资源类型)。在提交前事件期间删​​除ChoiceType变换器

在我已经在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:如何在预先提交事件期间取消验证? (仅在我的领域如果可能的话)

回答

0

找到了解决办法。我走错了方向。

所有我需要做的就是重写ChoiceType类和禁止ViewTransformers,然后使用新类:

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

添加一些jQuery和的.ignore类添加到您的具体形式领域。如果你加载整个表单,我建议单独加载它们,这样你可以轻松地添加它。

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

隐藏也将确保你做任何领域的隐藏,对于其他原因不会affacted,然后给出错误,参考:here

+1

不工作。这个问题来自Symfony的验证,而不是Javascript和HTML。 – Mouke