Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deleted elements before submit to avoid triggering validation #414

Conversation

jordisala1991
Copy link
Member

@jordisala1991 jordisala1991 commented Apr 10, 2023

Subject

I am targeting this branch, because this is a bug fix.

Closes sonata-project/SonataAdminBundle#4348.

Changelog

### Fixed
- Do not trigger validation on deleted rows of CollectionType

@jordisala1991
Copy link
Member Author

I have tried on a project and now the validation is not triggered when the collection item is marked for deletion.

WDYT @VincentLanglet ?

@VincentLanglet
Copy link
Member

VincentLanglet commented Apr 10, 2023

I have tried on a project and now the validation is not triggered when the collection item is marked for deletion.

WDYT @VincentLanglet ?

The bug was rely annoying, this is a great fix.
Do you think we have a way to add test here or in sonataAdmin ?

I don't have a project to reproduce the bug and use the fix.

I'm ok merging and releasing this

VincentLanglet
VincentLanglet previously approved these changes Apr 10, 2023
@jordisala1991
Copy link
Member Author

I have tried on a project and now the validation is not triggered when the collection item is marked for deletion.
WDYT @VincentLanglet ?

The bug was rely annoying, this is a great fix. Do you think we have a way to add test here or in sonataAdmin ?

I don't have a project to reproduce the bug and use the fix.

I'm ok merging and releasing this

It is really problematic to test this properly... form type is here, base templates are on the admin-bundle, BUT the form type widget is on the persistence bundles...

So I guess the only way to test this is on the DoctrineORM/MongoDB persistence bundles, I can try it once released.

VincentLanglet
VincentLanglet previously approved these changes Apr 10, 2023
@jordisala1991
Copy link
Member Author

Let me try one more way to delete those items. Wait a bit for the merge.

@jordisala1991
Copy link
Member Author

jordisala1991 commented Apr 10, 2023

No more changes needed, this is the cleaner solution I could found.

I also tried to make it work somehow like this: https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php

Where on preSubmit it only removed from the $form object, and on the submit it removed from the $data object, but it does not work on our use case. It complains about extra fields (data submitted contains more things that the $form is expecting, because we remove from $form first).

There should be more pieces on Symfony code dealing with that, but I think it is not worth investigating more.

Current behavior is buggy since it does not let you remove invalid items, and the fix improves that.

EDIT: I guess the resizer from Symfony is expecting that when you remove an item, the $data will not contain it, it does not rely on a checkbox for removal, but the full removal on the frontend prior submit, that's why it works this way.

@jordisala1991
Copy link
Member Author

I think this is the most correct solution @VincentLanglet . on Pre Submit we should make sure to remove extra data (data marked for removal) and to remove the form fields for that data (avoid adding them back to the form).

Then onSubmit we don't have:

  • Data submitted because we previously filter it
  • Form associated fields because it was also removed

Validation will work for fields not removed, but will not be triggered for removed fields.

@jordisala1991
Copy link
Member Author

I will try to prepare a test now for the doctrine orm admin bundle.

@jordisala1991
Copy link
Member Author

Something like this: sonata-project/SonataDoctrineORMAdminBundle#1737

@VincentLanglet VincentLanglet merged commit bd639e5 into sonata-project:1.x Apr 10, 2023
@jordisala1991 jordisala1991 deleted the hotfix/remove-deleted-elements-before-submit branch April 10, 2023 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sonata type collection delete row triggers constraints validation
2 participants