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:
Pierre Goiffon
2023-06-28 17:56:04 +02:00
parent d085f15b6d
commit 6cc2d49cd5
20 changed files with 526 additions and 172 deletions

View File

@@ -18,23 +18,23 @@ abstract class AbstractRegexpValidator extends AbstractValidator
protected string $sRegExp;
public function __construct(?string $sErrorMessage = null)
{
$this->sRegExp = static::DEFAULT_REGEXP;
parent::__construct($sErrorMessage);
}
public function __construct(?string $sErrorMessage = null)
{
$this->sRegExp = static::DEFAULT_REGEXP;
parent::__construct($sErrorMessage);
}
public function Validate($value): array
{
if (is_null($value)) {
$value = ''; // calling preg_match with null as subject is deprecated since PHP 8.1
}
if (preg_match($this->GetRegExp(true), $value)) {
return [true, null];
}
public function Validate($value): array
{
if (is_null($value)) {
$value = ''; // calling preg_match with null as subject is deprecated since PHP 8.1
}
if (preg_match($this->GetRegExp(true), $value)) {
return [];
}
return [false, $this->sErrorMessage];
}
return [$this->sErrorMessage];
}
/**
* Returns the regular expression of the validator.

View File

@@ -20,19 +20,20 @@ abstract class AbstractValidator
/** @var string message / dict key to use when an error occurs */
protected string $sErrorMessage;
public function __construct(?string $sErrorMessage)
public function __construct(?string $sErrorMessage = null)
{
if (false === utils::IsNullOrEmptyString($sErrorMessage)) {
$this->sErrorMessage = $sErrorMessage;
} else {
$this->sErrorMessage = self::DEFAULT_ERROR_MESSAGE;
}
else {
$this->sErrorMessage = static::DEFAULT_ERROR_MESSAGE;
}
}
/**
* @param mixed $value
*
* @return array<bool,?string> boolean valid for valid / invalid, and error message if invalid
* @return string[] list of error messages, empty array if no error
*/
abstract public function Validate($value): array;

View File

@@ -19,12 +19,56 @@
namespace Combodo\iTop\Form\Validator;
use Dict;
use ormLinkSet;
use utils;
/**
* Description of LinkedSetValidator
*
* @since 3.1
* @since 3.1.0 N°6414
*/
class LinkedSetValidator extends AbstractRegexpValidator
{
public const VALIDATOR_NAME = 'LinkedSetValidator';
public const VALIDATOR_NAME = 'linkedset_validator';
private $aAttributesToDisplayCodes;
public function __construct($aAttributesToDisplayCodes)
{
$this->aAttributesToDisplayCodes = $aAttributesToDisplayCodes;
parent::__construct();
}
public function Validate($value): array
{
$aErrorMessages = [];
/** @var ormLinkSet $oSet */
$oSet = $value;
// validate each links...
/** @var \DBObject $oItem */
foreach ($oSet as $oItem) {
$aChanges = $oItem->ListChanges();
foreach ($aChanges as $sAttCode => $AttValue) {
if (!in_array($sAttCode, $this->aAttributesToDisplayCodes)) {
continue;
}
$res = $oItem->CheckValue($sAttCode);
if ($res !== true) {
$sAttLabel = $oItem->GetLabel($sAttCode);
$sItem = utils::IsNullOrEmptyString($oItem->Get('friendlyname'))
? Dict::S('UI:Links:NewItem')
: $oItem->Get('friendlyname');
$sIssue = Dict::Format('Core:CheckValueError', $sAttLabel, $sAttCode, $res);
$aErrorMessages[] = '<b>' . $sItem . ' : </b>' . $sIssue;
}
}
}
$oSet->Rewind();
return $aErrorMessages;
}
}

View 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";
}
}
}

View 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 [];
}
}