EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.09k stars 1.03k forks source link

How to validate entity on create/update #5417

Open TomaIvanovTomov opened 2 years ago

TomaIvanovTomov commented 2 years ago

How to validate my entity? I want to check if the url property is unique. So I did this: My Post.php entity:

use Symfony\Component\Validator\Constraints as Assert;
....
    #[ORM\Column(length: 255)]
    #[Assert\Unique]
    private ?string $url = null;
....

This is obviously not enough because my entity is not validated on create/update. So I tried to add a group like:

....
#[Assert\GroupSequence(['post_validation'])]
class Post
....

And call the group in the PostGroupController.php:

public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setEntityLabelInSingular($this->translator->trans('post.post'))
            ->setEntityLabelInPlural($this->translator->trans('post.posts'))
            ->setSearchFields(['title'])
            ->setFormOptions(['validation_groups' => 'post_validation']);
    }

But this doesn't work either. By not working I mean that the record is saved/updated with not unique url. So what is wrong with this EasyAdmin validation or I am doing the things wrong ? P.S. I have the symfony validator installed.

parijke commented 2 years ago

This isn't a EA question. But to answer it, you have to set it on Entity level. Like this: #[UniqueEntity('policyNumber', message: 'Er is waarschijnlijk al een polis met dit nummer')]

TomaIvanovTomov commented 2 years ago

Thank you very much!