mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-21 09:38:48 +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:
45
sources/Form/Validator/SelectObjectValidator.php
Normal file
45
sources/Form/Validator/SelectObjectValidator.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/*
|
||||
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Form\Validator;
|
||||
|
||||
use Combodo\iTop\Form\Helper\FieldHelper;
|
||||
use DBSearch;
|
||||
use utils;
|
||||
|
||||
class SelectObjectValidator extends AbstractValidator
|
||||
{
|
||||
public const VALIDATOR_NAME = 'select_object_validator';
|
||||
|
||||
/** @var \DBSearch $oSearch */
|
||||
private $oSearch;
|
||||
|
||||
public function __construct(DBSearch $oSearch)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->oSearch = $oSearch;
|
||||
}
|
||||
|
||||
public function Validate($value): array
|
||||
{
|
||||
if (utils::IsNullOrEmptyString($value)) {
|
||||
return [];
|
||||
}
|
||||
if (($value === 0) || ($value === '0')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$oSetForExistingCurrentValue = FieldHelper::GetObjectsSetFromSearchAndCurrentValueId($this->oSearch, $value);
|
||||
$iObjectsCount = $oSetForExistingCurrentValue->CountWithLimit(1);
|
||||
|
||||
if ($iObjectsCount === 0) {
|
||||
return ["Value $value does not match the corresponding filter set"];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user