mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 23:44:11 +01:00
- 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
59 lines
1.6 KiB
PHP
59 lines
1.6 KiB
PHP
<?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";
|
|
}
|
|
}
|
|
} |