SonataAdminBundle: Sonata type collection delete row triggers constraints validation

I have a problem with sonata admin, specifically in a type collection field. Here’s the scenario:

I have an entity called “Group” with a one to many relationship to the entity “Member”

In sonata admin I set up the admin classes for the 2 entities.

In the “Group admin” i create a collection type with the Member entity this way

->add('members', 'sonata_type_collection', [
    'by_reference'          => false,
    'label'                 => 'Members',
    'type_options'          => ['delete' => true],
    'btn_add'               => "Add Member",
    'required'              => false,
    'constraints'           => $validation['members'],
],
[
    'edit'              => 'inline',
    'inline'            => 'table'
])

The member entity has 4 fields, one of which is required.

->add('firstName', TextType::class, [
    'label'         => 'First Name'
    'constraints'   => [
        new NotBlank(['message' => 'Please enter the name.']),
    ]
])

If I click the “Add Member” button in the edit view, it adds a new row as expected. At that point, if I change my mind and decide to delete the newly added record, without adding the name, on save it’s returning a validation error telling me ‘Please enter the name.’ on the field.

Shouldn’t the delete action have priority over validation?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 27 (23 by maintainers)

Most upvoted comments

Im suggest solving in any issue: Before set data we delete fisrt. @VincentLanglet you are closed not that issue. Problem solving here: https://github.com/sonata-project/SonataAdminBundle/issues/6391#issuecomment-709232369

Well if you remove the input immediately when checking the checkbox (or cliking a button), problem solved. Also, you could just make toggle disable on checking the checkbox

IMO the problem is, add is done in ajax, remove is done on reload… not sure if this has an easy fix tho.