From e5b5f081535ba71b7172359d2c518a8f730e8f3a Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Wed, 21 Jun 2017 11:26:13 +0200 Subject: [PATCH] FEATURE: Support validation groups as done by flow itself. * Provide another option validationGroups. * Allow each validation entry to be executed only for matching validation groups. * Necessary for the same reason as documented in flow documentation. It should be possible to execute different validation rules for the same name or class, only at some points, e.g. at a specific action. --- .../Validator/SettingsValidator.php | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/Classes/Validation/Validator/SettingsValidator.php b/Classes/Validation/Validator/SettingsValidator.php index 112bdc4..40825a9 100644 --- a/Classes/Validation/Validator/SettingsValidator.php +++ b/Classes/Validation/Validator/SettingsValidator.php @@ -39,7 +39,8 @@ class SettingsValidator extends AbstractValidator * @var array */ protected $supportedOptions = [ - 'name' => ['', 'Set the name of the setting-array to use', 'string', false] + 'name' => ['', 'Set the name of the setting-array to use', 'string', false], + 'validationGroups' => [['Default'], 'Same as "Validation Groups" of Flow Framework.', 'array', false], ]; /** @@ -77,6 +78,12 @@ class SettingsValidator extends AbstractValidator $config = &$this->validations[$name]; foreach ($config as $validatorConfig) { + if (! $this->doesValidationGroupsMatch($validatorConfig)) { + continue; + } + + $this->handleValidationGroups($validatorConfig); + $validator = $this->validatorResolver->createValidator( $validatorConfig['validator'], $validatorConfig['options'] @@ -98,4 +105,33 @@ class SettingsValidator extends AbstractValidator } } } + + /** + * Check whether at least one configured group does match, if any is configured. + * + * @param array $validatorConfig + * @return bool + */ + protected function doesValidationGroupsMatch(array &$validatorConfig) + { + if (isset($validatorConfig['validationGroups']) + && count(array_intersect($validatorConfig['validationGroups'], $this->options['validationGroups'])) === 0 + ) { + return false; + } + + return true; + } + + /** + * Add validation groups for recursion if necessary. + * + * @param array $validatorConfig + */ + protected function handleValidationGroups(array &$validatorConfig) + { + if ($validatorConfig['validator'] === 'DigiComp.SettingValidator:Settings' && empty($validatorConfig['options']['validationGroups'])) { + $validatorConfig['options']['validationGroups'] = $this->options['validationGroups']; + } + } }