https://github.com/rdey/entity-exists-validation-constraint
https://github.com/rdey/entity-exists-validation-constraint
lib php72 php73 php74 php80 php81 php82 sf63
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/rdey/entity-exists-validation-constraint
- Owner: rdey
- License: mit
- Fork: true (Happyr/entity-exists-validation-constraint)
- Created: 2022-07-18T11:52:24.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-07-18T12:21:55.000Z (over 3 years ago)
- Last Synced: 2024-09-29T19:41:47.848Z (over 1 year ago)
- Topics: lib, php72, php73, php74, php80, php81, php82, sf63
- Size: 14.6 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Symfony validator for Entity Exist
[](https://github.com/happyr/entity-exists-validation-constraint/releases)
[](LICENSE)
[](https://travis-ci.org/happyr/entity-exists-validation-constraint)
[](https://scrutinizer-ci.com/g/happyr/entity-exists-validation-constraint)
[](https://scrutinizer-ci.com/g/happyr/entity-exists-validation-constraint)
[](https://packagist.org/packages/happyr/entity-exists-validation-constraint)
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.
```php
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](https://symfony.com/doc/current/validation/sequence_provider.html) in order to avoid 500 errors from Doctrine mapping.
```php
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
```console
composer require happyr/entity-exists-validation-constraint
```
Then register the services with:
```yaml
# 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`.