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.
This commit is contained in:
Daniel Siepmann 2017-06-21 11:26:13 +02:00
parent 40fc82e515
commit e5b5f08153
No known key found for this signature in database
GPG key ID: 33D6629915560EF4

View file

@ -39,7 +39,8 @@ class SettingsValidator extends AbstractValidator
* @var array * @var array
*/ */
protected $supportedOptions = [ 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]; $config = &$this->validations[$name];
foreach ($config as $validatorConfig) { foreach ($config as $validatorConfig) {
if (! $this->doesValidationGroupsMatch($validatorConfig)) {
continue;
}
$this->handleValidationGroups($validatorConfig);
$validator = $this->validatorResolver->createValidator( $validator = $this->validatorResolver->createValidator(
$validatorConfig['validator'], $validatorConfig['validator'],
$validatorConfig['options'] $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'];
}
}
} }