mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
entity person: create a validator to check a person entity is linked
with a center This validator take a parameter in configuration
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\Person;
|
||||
|
||||
class PersonHasCenter extends \Symfony\Component\Validator\Constraint
|
||||
{
|
||||
public string $message = "A center is required";
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
return [
|
||||
self::CLASS_CONSTRAINT
|
||||
];
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\Person;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
class PersonHasCenterValidator extends \Symfony\Component\Validator\ConstraintValidator
|
||||
{
|
||||
private bool $centerRequired;
|
||||
|
||||
public function __construct(ParameterBagInterface $parameterBag)
|
||||
{
|
||||
$this->centerRequired = $parameterBag->get('chill_person')['validation']['center_required'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function validate($person, Constraint $constraint)
|
||||
{
|
||||
if (!$person instanceof Person) {
|
||||
throw new UnexpectedTypeException($constraint, Person::class);
|
||||
}
|
||||
|
||||
if (!$this->centerRequired) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (NULL === $person->getCenter()) {
|
||||
$this
|
||||
->context
|
||||
->buildViolation($constraint->message)
|
||||
->atPath('center')
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user