atournayre / entity-validation
Entity validation using Symfony Validator
Installs: 1 368
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
- doctrine/collections: ^1.6
- symfony/form: ^5.4
- symfony/validator: ^5.4
Requires (Dev)
- symfony/phpunit-bridge: ^6.2
This package is auto-updated.
Last update: 2024-12-01 00:09:13 UTC
README
This component helps entity validation, but it also works for DTOs...
Install
Composer
composer require atournayre/entity-validation
Add validation to a class
Entity / DTO / ...
namespace App\Entity; use Atournayre\EntityValidation\ValidableEntityTrait; class YourEntity implements ValidableEntityInterface { // Your code use ValidableEntityTrait; }
Constraint
Create YourEntityConstraint
(name is important); it has to be located in the same directory of your Entity / DTO ...,
ConstraintValidator
Create YourEntityConstraintValidator
(name is important); it has to be located in the same directory of your Entity / DTO ...,
Example
ConstraintValidator
<?php namespace App\Entity\YourEntity; use Atournayre\EntityValidation\ConstraintViolationListCollectionBuilder; use Atournayre\EntityValidation\ConstraintValidator; use Atournayre\EntityValidation\ConstraintViolationListCollection; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints as Assert; class YourEntityConstraintValidator extends ConstraintValidator { /** * @throws \Exception */ public function __construct() { $this->throwExceptionIfConstraintClassDoNotExists(__CLASS__); } /** * @param YourEntity $value * @param Constraint|null $constraint * @return ConstraintViolationListCollection */ public function validate($value, Constraint $constraint = null): ConstraintViolationListCollection { $constraintViolationListCollectionBuilder = ConstraintViolationListCollectionBuilder::create(); if (!$value instanceof YourEntity) { return $constraintViolationListCollectionBuilder->build(); } return $constraintViolationListCollectionBuilder // Suppose YourEntity has method getEmail() ->add($value->getEmail(), new Assert\Email()) // You can also specify propertyPath to get error attached properly in the form //->add($value->getEmail(), new Assert\Email(), 'email') ->build(); } }
Usages
Standalone
$entity = new YourEntity(); // To perform manual validation /** @var ConstraintViolationListCollection $constraintViolationListCollection */ $constraintViolationListCollection = $entity->validate(); // Check if entity has violations $hasViolations = $constraintViolationListCollection->count() != 0; // To get only messages $messages = $constraintViolationListCollection->getMessages(); // To get only invalid properties $invalidProperties = $constraintViolationListCollection->getPropertiesPaths(); // To get only invalid properties without brackets $invalidProperties = $constraintViolationListCollection->getPropertiesPaths(true);
Symfony Forms
// Before $form->isValid(), call the line below, it will add errors to form for invalid values EntityValidationHelper::form($form);
Contributing
Of course, open source is fueled by everyone's ability to give just a little bit of their time for the greater good. If you'd like to see a feature or add some of your own happy words, awesome! Tou can request it - but creating a pull request is an even better way to get things done.
Either way, please feel comfortable submitting issues or pull requests: all contributions and questions are warmly appreciated :).