happyr/entity-exists-validation-constraint

Verify that your entity exists

Maintainers

👁 Nyholm

Package info

github.com/Happyr/entity-exists-validation-constraint

pkg:composer/happyr/entity-exists-validation-constraint

Fund package maintenance!

Nyholm

Statistics

Installs: 207 887

Dependents: 0

Suggesters: 0

Stars: 31

Open Issues: 4

1.3.0 2025-03-31 10:29 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 34c9f8bbe6149c89e0bd78a362b7ed98125ee704

  • Radoje Albijanic <radoje.albijanic.woop@gmail.com>
  • Tobias Nyholm <tobias.nyholm.woop@gmail.com>

README

👁 Latest Version
👁 Software License
👁 Build Status
👁 Code Coverage
👁 Quality Score
👁 Total Downloads

A small validator that verifies that an Entity actually exists. This is especially useful if you use Symfony Messenger component. Now you can safely validate the message and put it on a queue for processing later.

namespace App\Message\Command;

use Happyr\Validator\Constraint\EntityExist;
use Symfony\Component\Validator\Constraints as Assert;

final class EmailUser
{
 /**
 * @Assert\NotBlank
 * @EntityExist(entity="App\Entity\User")
 *
 * @var int User's id property
 */
 private $user;

 /**
 * @Assert\NotBlank
 * @EntityExist(entity="App\Entity\Other", property="name")
 *
 * @var string The name of "Other". We use its "name" property. 
 */
 private $other;

 // ...

In case you are using other constraints to validate the property before entity should be checked in the database (like @Assert\Uuid) you should use Group sequence in order to avoid 500 errors from Doctrine mapping.

namespace App\Message\Command;

use Happyr\Validator\Constraint\EntityExist;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\GroupSequence({"EmailUser", "DatabaseCall"})
 */
final class EmailUser
{
 /**
 * @Assert\NotBlank
 * @Assert\Uuid
 * @EntityExist(entity="App\Entity\User", groups={"DatabaseCall"}, property="uuid")
 *
 * @var string Uuid
 */
 private $user;

 // ...

Install

composer require happyr/entity-exists-validation-constraint

Then register the services with:

# config/packages/happyr_entity_exists_validator.yaml
services:
 Happyr\Validator\Constraint\EntityExistValidator:
 arguments: ['@doctrine.orm.entity_manager']
 tags: [ 'validator.constraint_validator' ]

Note

The Validator will not produce a violation when value is empty. This means that you should most likely use it in combination with NotBlank.