EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Validation errors on filters

Open cve opened this issue 4 years ago • 12 comments

Describe the bug I want to filter Offer entity by their Product entity without validation errors

To Reproduce I'm using EasyAdmin 3. I have Product, VatRate and Offer entities. Product entity has ManyToOne VatRate with NotBlank constraint and OneToMany Offer entity. When I want to display list of Offers with specific Product then in filters at Product field I see "This value should not be blank" (because VatRate is required on Product entity, but it should not be required on filters)

(OPTIONAL) Additional context I'm trying to decorate EasyCorp\Bundle\EasyAdminBundle\Factory\FormFactory to add 'validation_grops' => ['Admin'] at createFiltersForm but this not work, because FormFactory type hint does not have an interface and fails in EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository

cve avatar Oct 09 '21 09:10 cve

I'm pretty sure that if you want to modify validation groups you'll use the getCrud -> newForm/editForm options instead.

pkly avatar Oct 09 '21 21:10 pkly

Thanks @pkly, but the problem is in filters form, which is in list view.

cve avatar Oct 15 '21 06:10 cve

oh, my bad why would that be even displayed if all it's supposed to be is just applying filters to the index query? this is kinda weird, can you share your configureFilters method and possibly other code you changed?

pkly avatar Oct 15 '21 06:10 pkly

I've checked that if I remove @Assert\NotBlank from my vatRate (ManyToOne VatRate) in Product then it works. I think this is because FormFactory has $form->isValid()

cve avatar Oct 15 '21 06:10 cve

doesn't that just mean that your entity is invalid in the first place?

pkly avatar Oct 15 '21 06:10 pkly

I'll prepare fork from admin demo for that

cve avatar Oct 15 '21 07:10 cve

Ok, I have that, please clone this https://github.com/cve/easyadmin-demo/tree/filter_not_valid, run and try to use author filter on blog posts and you should see that: image

cve avatar Oct 16 '21 09:10 cve

@pkly Did You see that?

cve avatar Oct 27 '21 07:10 cve

No, sorry, I'm currently busy with work and cannot check this. But like I said before, my guess is that the entities you're using are just invaliid, that is their validation rules are not matching up with the expected state that was persisted, and this you're getting this error since it's dubbling up from the entity. Btw you shouldn't put things like @Assert\NotBlank() on ManyToOne afaik.

https://github.com/cve/easyadmin-demo/blob/9bbeb25539d3606150776c76e27ba15d88d14eb0/src/Entity/User.php#L86

I'm guessing this error occurs since you have nothing there, or in some other field that has the @Assert\NotBlank annotation.

pkly avatar Oct 28 '21 07:10 pkly

Today encountered same issue. I think the biggest problem here is that "Filter Form" does not define it's own Validation Group and it's caused that "Default" group is validated.

As an example we have EntityFilter (which is used to filter "parent" in category tree), which is checking entire entity including Callback constraints.

I think solution for this is to allow to configure "validation_groups" in \EasyCorp\Bundle\EasyAdminBundle\Dto\FilterConfigDto and later re-use it when building \EasyCorp\Bundle\EasyAdminBundle\Form\Type\FiltersFormType

oleg-andreyev avatar May 23 '22 09:05 oleg-andreyev

Fast solution could be the following: https://github.com/EasyCorp/EasyAdminBundle/issues/4842#issuecomment-1377644859

vlady777 avatar Jan 10 '23 18:01 vlady777