mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-26 12:08:47 +02:00
N°6414 Move existing AbstractSimpleField::Validate impl to custom validators
- LinkedSetField - SelectObjectField - MultipleChoicesField (warning this hierarchy contains non multiple value fields like SelectField !) Also change AbstractValidator::Validate signature : now we are returning an array of error messages, so that we can return multiple ones
This commit is contained in:
59
sources/Form/Validator/MultipleChoicesValidator.php
Normal file
59
sources/Form/Validator/MultipleChoicesValidator.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/*
|
||||
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Form\Validator;
|
||||
|
||||
use utils;
|
||||
|
||||
class MultipleChoicesValidator extends AbstractValidator
|
||||
{
|
||||
public const VALIDATOR_NAME = 'multiple_choices_validator';
|
||||
|
||||
/** @var array List of possible choices */
|
||||
private array $aChoices;
|
||||
|
||||
public function __construct(array $aChoices)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->aChoices = $aChoices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Warning can either be an array (if multiple values are allowed in the field) or a primitive : {@see \Combodo\iTop\Form\Field\MultipleChoicesField::GetCurrentValue()}
|
||||
*
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function Validate($value): array
|
||||
{
|
||||
$aErrorMessages = [];
|
||||
if (false === is_array($value)) {
|
||||
$this->CheckValueAgainstChoices($value, $aErrorMessages);
|
||||
|
||||
return $aErrorMessages;
|
||||
}
|
||||
|
||||
if (count($value) === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
/** @noinspection PhpUnusedLocalVariableInspection */
|
||||
foreach ($value as $sCode => $valueItem) {
|
||||
if (utils::IsNullOrEmptyString($valueItem)) {
|
||||
continue;
|
||||
}
|
||||
$this->CheckValueAgainstChoices($valueItem, $aErrorMessages);
|
||||
}
|
||||
|
||||
return $aErrorMessages;
|
||||
}
|
||||
|
||||
private function CheckValueAgainstChoices(string $sValue, array &$aErrorMessages): void
|
||||
{
|
||||
if (false === array_key_exists($sValue, $this->aChoices)) {
|
||||
$aErrorMessages[] = "Value ({$sValue}) is not part of the field possible values list";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user