【问题标题】:Symfony2 Form - "You cannot change the data of a submitted form."Symfony2 表单 - “您不能更改已提交表单的数据。”
【发布时间】:2016-05-30 09:56:58
【问题描述】:

我有一个表单,用户可以在其中选择首选付款方式。所选付款方式的名称写在隐藏字段“paymentMethod”中。提交表单后,系统会检查客户是否可以付款(付款合作伙伴的 API)。

检查确定:用户应该被重定向到确认页面(没问题)

检查失败:用户应该会再次看到表单,但隐藏字段paymentMethod必须设置为null,因此付款的选择框再次可见。

问题:您无法更改已提交表单的数据。

【问题讨论】:

标签: php jquery forms symfony


【解决方案1】:

解决方案非常简单:

我在我的 FormModel 中添加了一个函数:

  public function resetPaymentMethod() {
    $this->paymentMethod = null;
  }

【讨论】:

  • 需要详细说明吗?
【解决方案2】:

当您在已“提交”的表单上调用 setData() 时会发生此错误;

在这种情况下,要将 paymentMethod 设置为 null,请在您的 FormType 类中添加一个函数:

/** 
 * this gets called in the final stage before rendering the form
 *
 * @return void
 */   
public function finishView(FormView $view, FormInterface $form, array $options)
{
    // set paymentMethod here
}

【讨论】:

  • 但我不想在渲染之前将 paymentMethod 设置为 null 。只有当所选的付款方式对客户不可用时,它才应该为空。..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 2014-06-28
  • 1970-01-01
  • 2023-03-26
相关资源
最近更新 更多