Symfony 4 embed form with collectionType without doctrine “allow_delete” seems not to work correctly












0















For a specific project we use symfony without any ORM. We use a REST API to get and store data.
We have classes really similar to doctrine entities but it's a custom made service that get and store the data with the API.



For a really complex page we have some repeatable embed forms. Everything works fine except the delete process.



When we delete an item (by removing the fields in the DOM with JavaScript) in the embed collection and submit, the form data still contains the row of the deleted item but all the properties are null. Exactly as if we had forgotten to use the "allow_delete" property on the CollectionType field.



Can you please give us some help ?



The main object class :



class Survey extends WSObject
{
protected $id;
protected $title;
protected $surveyQuestions;

//Getter and setters are ok
}


The "child" object class :



class SurveyQuestion extends WSObject
{
protected $id;
protected $title;
protected $surveyId;

//Getter and setters are ok
}


The form construction :



// $survey is a well constructed survey with data
$form = $this->createFormBuilder($survey)
->add('title', TextType::class,array(
'label' => 'Title FR *',
'required' => true
))
->add('surveyQuestions', CollectionType::class, array(
'entry_type' => SurveyQuestionType::class,
'allow_add' => true,
'allow_delete' => true,
'delete_empty'=>true,
'label' => false,
'entry_options' => array(
'surveyId'=>$survey->getId(),
'empty_data' => null
)
))
->getForm();


The SurveyQuestionType :



use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;

use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeHiddenType;

class SurveyQuestionType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', HiddenType::class,array(
'required' => false
));
$builder->add('title', TextType::class,array(
'required' => true
));
$builder->add('surveyId', HiddenType::class,array(
'data' => $options['surveyId'],
'required' => false
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => SurveyQuestion::class,
'surveyId' => null
));
}
}




Imagine an existing survey with already two questions. If we remove one question in the form (by deleting fields from the DOM in JavaScript) and submit. The $form->getData(); returns :



Survey {
id : 42,
title : 'the meaning of life',
surveyQuestions : array(2) [
0 : surveyQuestion {
id : 1,
title : 'My first question',
surveyId : 42
},
// This is the corresponding deleted row
1 : surveyQuestion {
id : null,
title : null,
surveyId : null
},
]
}


What we expect is :



Survey {
id : 42,
title : 'the meaning of life',
surveyQuestions : array(1) [
0 : surveyQuestion {
id : 1,
title : 'My first question',
surveyId : 42
}
]
}


If you need any more details please let me know. Thanks in advance.
Best regards.










share|improve this question





























    0















    For a specific project we use symfony without any ORM. We use a REST API to get and store data.
    We have classes really similar to doctrine entities but it's a custom made service that get and store the data with the API.



    For a really complex page we have some repeatable embed forms. Everything works fine except the delete process.



    When we delete an item (by removing the fields in the DOM with JavaScript) in the embed collection and submit, the form data still contains the row of the deleted item but all the properties are null. Exactly as if we had forgotten to use the "allow_delete" property on the CollectionType field.



    Can you please give us some help ?



    The main object class :



    class Survey extends WSObject
    {
    protected $id;
    protected $title;
    protected $surveyQuestions;

    //Getter and setters are ok
    }


    The "child" object class :



    class SurveyQuestion extends WSObject
    {
    protected $id;
    protected $title;
    protected $surveyId;

    //Getter and setters are ok
    }


    The form construction :



    // $survey is a well constructed survey with data
    $form = $this->createFormBuilder($survey)
    ->add('title', TextType::class,array(
    'label' => 'Title FR *',
    'required' => true
    ))
    ->add('surveyQuestions', CollectionType::class, array(
    'entry_type' => SurveyQuestionType::class,
    'allow_add' => true,
    'allow_delete' => true,
    'delete_empty'=>true,
    'label' => false,
    'entry_options' => array(
    'surveyId'=>$survey->getId(),
    'empty_data' => null
    )
    ))
    ->getForm();


    The SurveyQuestionType :



    use SymfonyComponentFormAbstractType;
    use SymfonyComponentFormFormBuilderInterface;
    use SymfonyComponentOptionsResolverOptionsResolver;

    use SymfonyComponentFormExtensionCoreTypeTextType;
    use SymfonyComponentFormExtensionCoreTypeHiddenType;

    class SurveyQuestionType extends AbstractType
    {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder->add('id', HiddenType::class,array(
    'required' => false
    ));
    $builder->add('title', TextType::class,array(
    'required' => true
    ));
    $builder->add('surveyId', HiddenType::class,array(
    'data' => $options['surveyId'],
    'required' => false
    ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
    $resolver->setDefaults(array(
    'data_class' => SurveyQuestion::class,
    'surveyId' => null
    ));
    }
    }




    Imagine an existing survey with already two questions. If we remove one question in the form (by deleting fields from the DOM in JavaScript) and submit. The $form->getData(); returns :



    Survey {
    id : 42,
    title : 'the meaning of life',
    surveyQuestions : array(2) [
    0 : surveyQuestion {
    id : 1,
    title : 'My first question',
    surveyId : 42
    },
    // This is the corresponding deleted row
    1 : surveyQuestion {
    id : null,
    title : null,
    surveyId : null
    },
    ]
    }


    What we expect is :



    Survey {
    id : 42,
    title : 'the meaning of life',
    surveyQuestions : array(1) [
    0 : surveyQuestion {
    id : 1,
    title : 'My first question',
    surveyId : 42
    }
    ]
    }


    If you need any more details please let me know. Thanks in advance.
    Best regards.










    share|improve this question



























      0












      0








      0








      For a specific project we use symfony without any ORM. We use a REST API to get and store data.
      We have classes really similar to doctrine entities but it's a custom made service that get and store the data with the API.



      For a really complex page we have some repeatable embed forms. Everything works fine except the delete process.



      When we delete an item (by removing the fields in the DOM with JavaScript) in the embed collection and submit, the form data still contains the row of the deleted item but all the properties are null. Exactly as if we had forgotten to use the "allow_delete" property on the CollectionType field.



      Can you please give us some help ?



      The main object class :



      class Survey extends WSObject
      {
      protected $id;
      protected $title;
      protected $surveyQuestions;

      //Getter and setters are ok
      }


      The "child" object class :



      class SurveyQuestion extends WSObject
      {
      protected $id;
      protected $title;
      protected $surveyId;

      //Getter and setters are ok
      }


      The form construction :



      // $survey is a well constructed survey with data
      $form = $this->createFormBuilder($survey)
      ->add('title', TextType::class,array(
      'label' => 'Title FR *',
      'required' => true
      ))
      ->add('surveyQuestions', CollectionType::class, array(
      'entry_type' => SurveyQuestionType::class,
      'allow_add' => true,
      'allow_delete' => true,
      'delete_empty'=>true,
      'label' => false,
      'entry_options' => array(
      'surveyId'=>$survey->getId(),
      'empty_data' => null
      )
      ))
      ->getForm();


      The SurveyQuestionType :



      use SymfonyComponentFormAbstractType;
      use SymfonyComponentFormFormBuilderInterface;
      use SymfonyComponentOptionsResolverOptionsResolver;

      use SymfonyComponentFormExtensionCoreTypeTextType;
      use SymfonyComponentFormExtensionCoreTypeHiddenType;

      class SurveyQuestionType extends AbstractType
      {

      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder->add('id', HiddenType::class,array(
      'required' => false
      ));
      $builder->add('title', TextType::class,array(
      'required' => true
      ));
      $builder->add('surveyId', HiddenType::class,array(
      'data' => $options['surveyId'],
      'required' => false
      ));
      }

      public function configureOptions(OptionsResolver $resolver)
      {
      $resolver->setDefaults(array(
      'data_class' => SurveyQuestion::class,
      'surveyId' => null
      ));
      }
      }




      Imagine an existing survey with already two questions. If we remove one question in the form (by deleting fields from the DOM in JavaScript) and submit. The $form->getData(); returns :



      Survey {
      id : 42,
      title : 'the meaning of life',
      surveyQuestions : array(2) [
      0 : surveyQuestion {
      id : 1,
      title : 'My first question',
      surveyId : 42
      },
      // This is the corresponding deleted row
      1 : surveyQuestion {
      id : null,
      title : null,
      surveyId : null
      },
      ]
      }


      What we expect is :



      Survey {
      id : 42,
      title : 'the meaning of life',
      surveyQuestions : array(1) [
      0 : surveyQuestion {
      id : 1,
      title : 'My first question',
      surveyId : 42
      }
      ]
      }


      If you need any more details please let me know. Thanks in advance.
      Best regards.










      share|improve this question
















      For a specific project we use symfony without any ORM. We use a REST API to get and store data.
      We have classes really similar to doctrine entities but it's a custom made service that get and store the data with the API.



      For a really complex page we have some repeatable embed forms. Everything works fine except the delete process.



      When we delete an item (by removing the fields in the DOM with JavaScript) in the embed collection and submit, the form data still contains the row of the deleted item but all the properties are null. Exactly as if we had forgotten to use the "allow_delete" property on the CollectionType field.



      Can you please give us some help ?



      The main object class :



      class Survey extends WSObject
      {
      protected $id;
      protected $title;
      protected $surveyQuestions;

      //Getter and setters are ok
      }


      The "child" object class :



      class SurveyQuestion extends WSObject
      {
      protected $id;
      protected $title;
      protected $surveyId;

      //Getter and setters are ok
      }


      The form construction :



      // $survey is a well constructed survey with data
      $form = $this->createFormBuilder($survey)
      ->add('title', TextType::class,array(
      'label' => 'Title FR *',
      'required' => true
      ))
      ->add('surveyQuestions', CollectionType::class, array(
      'entry_type' => SurveyQuestionType::class,
      'allow_add' => true,
      'allow_delete' => true,
      'delete_empty'=>true,
      'label' => false,
      'entry_options' => array(
      'surveyId'=>$survey->getId(),
      'empty_data' => null
      )
      ))
      ->getForm();


      The SurveyQuestionType :



      use SymfonyComponentFormAbstractType;
      use SymfonyComponentFormFormBuilderInterface;
      use SymfonyComponentOptionsResolverOptionsResolver;

      use SymfonyComponentFormExtensionCoreTypeTextType;
      use SymfonyComponentFormExtensionCoreTypeHiddenType;

      class SurveyQuestionType extends AbstractType
      {

      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder->add('id', HiddenType::class,array(
      'required' => false
      ));
      $builder->add('title', TextType::class,array(
      'required' => true
      ));
      $builder->add('surveyId', HiddenType::class,array(
      'data' => $options['surveyId'],
      'required' => false
      ));
      }

      public function configureOptions(OptionsResolver $resolver)
      {
      $resolver->setDefaults(array(
      'data_class' => SurveyQuestion::class,
      'surveyId' => null
      ));
      }
      }




      Imagine an existing survey with already two questions. If we remove one question in the form (by deleting fields from the DOM in JavaScript) and submit. The $form->getData(); returns :



      Survey {
      id : 42,
      title : 'the meaning of life',
      surveyQuestions : array(2) [
      0 : surveyQuestion {
      id : 1,
      title : 'My first question',
      surveyId : 42
      },
      // This is the corresponding deleted row
      1 : surveyQuestion {
      id : null,
      title : null,
      surveyId : null
      },
      ]
      }


      What we expect is :



      Survey {
      id : 42,
      title : 'the meaning of life',
      surveyQuestions : array(1) [
      0 : surveyQuestion {
      id : 1,
      title : 'My first question',
      surveyId : 42
      }
      ]
      }


      If you need any more details please let me know. Thanks in advance.
      Best regards.







      php symfony symfony-forms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 20:56







      Gulvar

















      asked Nov 22 '18 at 20:36









      GulvarGulvar

      114




      114
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437725%2fsymfony-4-embed-form-with-collectiontype-without-doctrine-allow-delete-seems-n%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437725%2fsymfony-4-embed-form-with-collectiontype-without-doctrine-allow-delete-seems-n%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen