Update DefaultFormFactory extension point. Use array for validators

This commit is contained in:
cpenny 2020-02-20 08:54:30 +13:00
parent 11e2005b9b
commit a2b57f0801
2 changed files with 45 additions and 22 deletions

View File

@ -101,12 +101,17 @@ class DefaultFormFactory implements FormFactory
return null; return null;
} }
$validator = $context['Record']->getValidatorList(); $validatorList = $context['Record']->getValidatorList();
// Extend validator // Extend validator - legacy support, will be removed in 5.0.0
$this->invokeWithExtensions('updateFormValidator', $validator, $controller, $name, $context); foreach ($validatorList->getValidators() as $validator) {
$this->invokeWithExtensions('updateFormValidator', $validator, $controller, $name, $context);
}
return $validator; // Extend validator - forward support, will be supported beyond 5.0.0
$this->invokeWithExtensions('updateValidatorList', $validatorList);
return $validatorList;
} }
/** /**

View File

@ -2,7 +2,7 @@
namespace SilverStripe\Forms; namespace SilverStripe\Forms;
use SilverStripe\ORM\ArrayList; use InvalidArgumentException;
use SilverStripe\ORM\ValidationResult; use SilverStripe\ORM\ValidationResult;
/** /**
@ -13,13 +13,18 @@ use SilverStripe\ORM\ValidationResult;
class ValidatorList extends Validator class ValidatorList extends Validator
{ {
/** /**
* @var ArrayList|Validator[] * @var array|Validator[]
*/ */
private $validators; private $validators;
public function __construct() /**
* ValidatorList constructor.
*
* @param array|Validator[] $validators
*/
public function __construct(array $validators = [])
{ {
$this->validators = ArrayList::create(); $this->validators = array_values($validators);
parent::__construct(); parent::__construct();
} }
@ -43,7 +48,7 @@ class ValidatorList extends Validator
*/ */
public function addValidator(Validator $validator): ValidatorList public function addValidator(Validator $validator): ValidatorList
{ {
$this->getValidators()->add($validator); $this->validators[] = $validator;
return $this; return $this;
} }
@ -129,27 +134,27 @@ class ValidatorList extends Validator
} }
/** /**
* @return ArrayList|Validator[] * @return array|Validator[]
*/ */
public function getValidators(): ArrayList public function getValidators(): array
{ {
return $this->validators; return $this->validators;
} }
/** /**
* @param string $className * @param string $className
* @return ArrayList|Validator[] * @return array|Validator[]
*/ */
public function getValidatorsByType(string $className): ArrayList public function getValidatorsByType(string $className): array
{ {
$validators = ArrayList::create(); $validators = [];
foreach ($this->getValidators() as $validator) { foreach ($this->getValidators() as $key => $validator) {
if (!$validator instanceof $className) { if (!$validator instanceof $className) {
continue; continue;
} }
$validators->add($validator); $validators[$key] = $validator;
} }
return $validators; return $validators;
@ -161,17 +166,30 @@ class ValidatorList extends Validator
*/ */
public function removeValidatorsByType(string $className): ValidatorList public function removeValidatorsByType(string $className): ValidatorList
{ {
foreach ($this->getValidators() as $validator) { foreach ($this->getValidatorsByType($className) as $key => $validator) {
if (!$validator instanceof $className) { $this->removeValidatorByKey($key);
continue;
}
$this->getValidators()->remove($validator);
} }
return $this; return $this;
} }
/**
* @param int $key
* @return ValidatorList
*/
public function removeValidatorByKey(int $key): ValidatorList
{
if (!array_key_exists($key, $this->validators)) {
throw new InvalidArgumentException(
sprintf('Key "%s" does not exist in $validators array', $key)
);
}
unset($this->validators[$key]);
return $this;
}
/** /**
* @return bool * @return bool
*/ */