Search code examples
formssymfonybundle

Symfony 3.4. overwrite class Symfony\Component\Form


i need to overwrite the function "public function submit($submittedData, $clearMissing = true)" in Symfony\Component\Form.

I can overwrite the "RequestHandlerInterface" but not the form component.

Background: There is a problem with choiceTypes if you submit with Patch-Method (github.com/symfony/symfony/issues/17799). So i want to insert this line into the submit function:

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

Solution

  • You can patch the Form class directly as suggested here: https://github.com/symfony/symfony/issues/17799#issuecomment-184473725


    The only problem is: How to make this change permanent for you project?

    I ended up creating a patch for the form class. On Linux and supposing you are using git it works something like this:

    1. Open a terminal in your project root directory

    2. Add the form class to your git repository

      git add vendor/symfony/symfony/src/Symfony/Component/Form/Form.php
      
    3. Patch the form class as described above

    4. Create a patch

      git diff > form.patch
      
    5. Remove the form class from you git repository

      git rm --cached vendor/symfony/symfony/src/Symfony/Component/Form/Form.php
      
    6. Somehow incorporate the patch in your project update process. For my project I wrote a small script that runs after every composer install or composer 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
      

    The options for the patch command are chosen such that there will be no interaction or other side effects in case the patch was already applied.


    form.patch itself looks something like this:

    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.