new ProjectConstraint to add dynamic project validation (#2747)
This commit is contained in:
@@ -65,6 +65,9 @@ services:
|
||||
App\Validator\Constraints\TimesheetValidator:
|
||||
arguments: [!tagged timesheet.validator]
|
||||
|
||||
App\Validator\Constraints\ProjectValidator:
|
||||
arguments: [!tagged project.validator]
|
||||
|
||||
App\Validator\Constraints\QuickEntryTimesheetValidator:
|
||||
arguments: [!tagged timesheet.validator]
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ use App\Saml\Security\SamlFactory;
|
||||
use App\Timesheet\CalculatorInterface as TimesheetCalculator;
|
||||
use App\Timesheet\Rounding\RoundingInterface;
|
||||
use App\Timesheet\TrackingMode\TrackingModeInterface;
|
||||
use App\Validator\Constraints\ProjectConstraint;
|
||||
use App\Validator\Constraints\TimesheetConstraint;
|
||||
use App\Widget\WidgetInterface;
|
||||
use App\Widget\WidgetRendererInterface;
|
||||
@@ -60,6 +61,7 @@ class Kernel extends BaseKernel
|
||||
public const TAG_TIMESHEET_EXPORTER = 'timesheet.exporter';
|
||||
public const TAG_TIMESHEET_TRACKING_MODE = 'timesheet.tracking_mode';
|
||||
public const TAG_TIMESHEET_ROUNDING_MODE = 'timesheet.rounding_mode';
|
||||
public const TAG_PROJECT_VALIDATOR = 'project.validator';
|
||||
|
||||
public function getCacheDir()
|
||||
{
|
||||
@@ -87,6 +89,7 @@ class Kernel extends BaseKernel
|
||||
$container->registerForAutoconfiguration(TrackingModeInterface::class)->addTag(self::TAG_TIMESHEET_TRACKING_MODE);
|
||||
$container->registerForAutoconfiguration(RoundingInterface::class)->addTag(self::TAG_TIMESHEET_ROUNDING_MODE);
|
||||
$container->registerForAutoconfiguration(TimesheetConstraint::class)->addTag(self::TAG_TIMESHEET_VALIDATOR);
|
||||
$container->registerForAutoconfiguration(ProjectConstraint::class)->addTag(self::TAG_PROJECT_VALIDATOR);
|
||||
|
||||
/** @var SecurityExtension $extension */
|
||||
$extension = $container->getExtension('security');
|
||||
|
||||
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