交响乐 3.4。覆盖 class Symfony\Component\Form

Symfony 3.4. overwrite class Symfony\Component\Form

我需要覆盖 Symfony\Component\Form 中的函数 "public function submit($submittedData, $clearMissing = true)"。

我可以覆盖 "RequestHandlerInterface" 但不能覆盖表单组件。

背景:如果您使用 Patch-Method (github.com/symfony/symfony/issues/17799) 提交,则 choiceTypes 会出现问题。所以我想将这一行插入到提交函数中:

$clearMissing = $this->getConfig()->getOption('expanded', false) ?: $clearMissing;

您可以按照此处的建议直接修补表单 class: https://github.com/symfony/symfony/issues/17799#issuecomment-184473725


唯一的问题是:如何使此更改对您的项目永久有效?

我最终为表单 class 创建了一个补丁。在 Linux 上,假设您使用的是 git,它的工作原理如下:

  1. 在你的项目根目录打开一个终端

  2. 将表单 class 添加到您的 git 存储库

    git add vendor/symfony/symfony/src/Symfony/Component/Form/Form.php
    
  3. 按上述方式修补表格 class

  4. 创建补丁

    git diff > form.patch
    
  5. 从您的 git 存储库中删除表单 class

    git rm --cached vendor/symfony/symfony/src/Symfony/Component/Form/Form.php
    
  6. 以某种方式将补丁合并到您的项目更新过程中。对于我的项目,我编写了一个小脚本,在每个 composer installcomposer update.

    之后运行
    #!/bin/sh
    
    # apply form component patch to fix HTTP PATCH issue with ChoiceType
    # run after composer install/update
    patch -p1 -N -r /dev/null < form.patch
    

选择补丁命令的选项,以便在补丁已经应用的情况下不会有交互作用或其他副作用。


form.patch 本身看起来像这样:

diff --git a/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php b/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php
index 67fd234f..27ed9e81 100644
--- a/www/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php
+++ b/www/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php
@@ -553,6 +553,10 @@ class Form implements \IteratorAggregate, FormInterface
                 $submittedData = $event->getData();
             }

+            // HTTP PATCH fix for ChoiceType
+            // https://github.com/symfony/symfony/issues/17799#issuecomment-184473725
+            $clearMissing = $this->getConfig()->getOption('expanded', false) ?: $clearMissing;
+
             // Check whether the form is compound.
             // This check is preferable over checking the number of children,
             // since forms without children may also be compound.