new ProjectConstraint to add dynamic project validation (#2747)
This commit is contained in:
19
src/Validator/Constraints/ProjectConstraint.php
Normal file
19
src/Validator/Constraints/ProjectConstraint.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Extend this class if you want to add dynamic project validation (eg. via a bundle).
|
||||
*/
|
||||
abstract class ProjectConstraint extends Constraint
|
||||
{
|
||||
}
|
||||
@@ -18,6 +18,19 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
class ProjectValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* @var Constraint[]
|
||||
*/
|
||||
private $constraints;
|
||||
|
||||
/**
|
||||
* @param Constraint[] $constraints
|
||||
*/
|
||||
public function __construct(iterable $constraints = [])
|
||||
{
|
||||
$this->constraints = $constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Project|mixed $value
|
||||
* @param Constraint $constraint
|
||||
@@ -33,6 +46,13 @@ class ProjectValidator extends ConstraintValidator
|
||||
}
|
||||
|
||||
$this->validateProject($value, $this->context);
|
||||
|
||||
foreach ($this->constraints as $constraint) {
|
||||
$this->context
|
||||
->getValidator()
|
||||
->inContext($this->context)
|
||||
->validate($value, $constraint, [Constraint::DEFAULT_GROUP]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateProject(Project $project, ExecutionContextInterface $context)
|
||||
|
||||
Reference in New Issue
Block a user