mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Update DefaultFormFactory extension point. Use array for validators
This commit is contained in:
parent
11e2005b9b
commit
a2b57f0801
@ -101,12 +101,17 @@ class DefaultFormFactory implements FormFactory
|
||||
return null;
|
||||
}
|
||||
|
||||
$validator = $context['Record']->getValidatorList();
|
||||
$validatorList = $context['Record']->getValidatorList();
|
||||
|
||||
// Extend validator
|
||||
$this->invokeWithExtensions('updateFormValidator', $validator, $controller, $name, $context);
|
||||
// Extend validator - legacy support, will be removed in 5.0.0
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace SilverStripe\Forms;
|
||||
|
||||
use SilverStripe\ORM\ArrayList;
|
||||
use InvalidArgumentException;
|
||||
use SilverStripe\ORM\ValidationResult;
|
||||
|
||||
/**
|
||||
@ -13,13 +13,18 @@ use SilverStripe\ORM\ValidationResult;
|
||||
class ValidatorList extends Validator
|
||||
{
|
||||
/**
|
||||
* @var ArrayList|Validator[]
|
||||
* @var array|Validator[]
|
||||
*/
|
||||
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();
|
||||
}
|
||||
@ -43,7 +48,7 @@ class ValidatorList extends Validator
|
||||
*/
|
||||
public function addValidator(Validator $validator): ValidatorList
|
||||
{
|
||||
$this->getValidators()->add($validator);
|
||||
$this->validators[] = $validator;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$validators->add($validator);
|
||||
$validators[$key] = $validator;
|
||||
}
|
||||
|
||||
return $validators;
|
||||
@ -161,17 +166,30 @@ class ValidatorList extends Validator
|
||||
*/
|
||||
public function removeValidatorsByType(string $className): ValidatorList
|
||||
{
|
||||
foreach ($this->getValidators() as $validator) {
|
||||
if (!$validator instanceof $className) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->getValidators()->remove($validator);
|
||||
foreach ($this->getValidatorsByType($className) as $key => $validator) {
|
||||
$this->removeValidatorByKey($key);
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user