N°6414 Validator refactoring

New AbstractValidator class, with new method Validate
All existing validators are now children of AbstractRegexpValidator
Handle validators JS counterparts in renderers : only regexp validators are implemented client side
This commit is contained in:
Pierre Goiffon
2023-06-29 10:41:51 +02:00
parent 52049b7837
commit 6606af71ff
26 changed files with 348 additions and 247 deletions

View File

@@ -8,8 +8,8 @@
namespace Combodo\iTop\Form\Field;
use Closure;
use Combodo\iTop\Form\Validator\AbstractValidator;
use Combodo\iTop\Form\Validator\MandatoryValidator;
use Combodo\iTop\Form\Validator\Validator;
/**
* Description of Field
@@ -68,7 +68,7 @@ abstract class Field
protected $bMandatory;
/** @var string */
protected $sDisplayMode;
/** @var array */
/** @var AbstractValidator[] */
protected $aValidators;
/**
* @var bool
@@ -228,10 +228,6 @@ abstract class Field
return $this->sDisplayMode;
}
/**
*
* @return array
*/
public function GetValidators()
{
return $this->aValidators;
@@ -352,38 +348,46 @@ abstract class Field
* Setting the value will automatically add/remove a MandatoryValidator to the Field
*
* @param boolean $bMandatory
*
* @return $this
*/
public function SetMandatory(bool $bMandatory)
{
// Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
if ($bMandatory && !$this->bMandatory)
{
$this->AddValidator(new MandatoryValidator());
if ($bMandatory && !$this->bMandatory) {
$this->AddValidator($this->GetMandatoryValidatorInstance());
}
if (!$bMandatory)
{
foreach ($this->aValidators as $iKey => $oValue)
{
if ($oValue::Getname() === MandatoryValidator::GetName())
{
unset($this->aValidators[$iKey]);
}
}
}
if (false === $bMandatory) {
foreach ($this->aValidators as $iKey => $oValue) {
if ($oValue instanceof MandatoryValidator) {
unset($this->aValidators[$iKey]);
}
}
}
$this->bMandatory = $bMandatory;
return $this;
}
$this->bMandatory = $bMandatory;
/**
* Sets if the field is must change or not.
* Note: This not implemented yet! Just a pre-conception for CaseLogField
*
* @todo Implement
* @param boolean $bMustChange
return $this;
}
/**
* @return AbstractValidator
* @since 3.1.0 N°6414
*/
protected function GetMandatoryValidatorInstance(): AbstractValidator
{
return new MandatoryValidator();
}
/**
* Sets if the field is must change or not.
* Note: This not implemented yet! Just a pre-conception for CaseLogField
*
* @param boolean $bMustChange
*
* @return $this
* @todo Implement
*/
public function SetMustChange(bool $bMustChange)
{
@@ -476,31 +480,28 @@ abstract class Field
return $this;
}
/**
*
* @param \Combodo\iTop\Form\Validator\Validator $oValidator
* @return $this
*/
public function AddValidator(Validator $oValidator)
/**
* @param AbstractValidator $oValidator
* @return $this
*/
public function AddValidator(AbstractValidator $oValidator)
{
$this->aValidators[] = $oValidator;
return $this;
}
/**
*
* @param \Combodo\iTop\Form\Validator\Validator $oValidator
* @return $this
*/
public function RemoveValidator(Validator $oValidator)
public function RemoveValidator(AbstractValidator $oValidator)
{
foreach ($this->aValidators as $iKey => $oValue)
{
if ($oValue === $oValidator)
{
foreach ($this->aValidators as $iKey => $oValue) {
if ($oValue === $oValidator) {
unset($this->aValidators[$iKey]);
}
}
return $this;
}
@@ -574,10 +575,13 @@ abstract class Field
}
/**
* Checks the validators to see if the field's current value is valid.
* Then sets $bValid and $aErrorMessages.
* Validates the field using the validators set.
*
* @return boolean
* Before overriding this method in children classes, try to add a custom validator !
*
* @uses GetValidators()
* @uses SetValid()
* @uses AddErrorMessage()
*/
public function Validate()
{
@@ -592,9 +596,12 @@ abstract class Field
if (!$bEmpty || $this->GetMandatory()) {
foreach ($this->GetValidators() as $oValidator) {
if (!preg_match($oValidator->GetRegExp(true), $this->GetCurrentValue())) {
[$bIsFieldValid, $sValidationErrorMessage] = $oValidator->Validate($this->GetCurrentValue());
/** @var bool $bIsFieldValid */
if (false === $bIsFieldValid) {
$this->SetValid(false);
$this->AddErrorMessage($oValidator->GetErrorMessage());
$this->AddErrorMessage($sValidationErrorMessage);
}
}
}

View File

@@ -22,6 +22,7 @@ namespace Combodo\iTop\Form\Field;
use BinaryExpression;
use Closure;
use Combodo\iTop\Form\Validator\AbstractValidator;
use Combodo\iTop\Form\Validator\NotEmptyExtKeyValidator;
use ContextTag;
use DBObjectSet;
@@ -168,38 +169,15 @@ class SelectObjectField extends Field
return $this;
}
/**
* @inheritDoc
*/
public function SetMandatory(bool $bMandatory)
protected function GetMandatoryValidatorInstance(): AbstractValidator
{
// Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
if ($bMandatory && !$this->bMandatory)
{
$this->AddValidator(new NotEmptyExtKeyValidator());
}
if (!$bMandatory)
{
foreach ($this->aValidators as $iKey => $oValue)
{
if ($oValue::Getname() === NotEmptyExtKeyValidator::GetName())
{
unset($this->aValidators[$iKey]);
}
}
}
$this->bMandatory = $bMandatory;
return $this;
return new NotEmptyExtKeyValidator();
}
/**
* @return \DBSearch
*/
public function GetSearch()
{
public function GetSearch() {
return $this->oSearch;
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Form\Validator;
/**
* @since 3.1.0 N°6414
*/
abstract class AbstractRegexpValidator extends AbstractValidator
{
public const VALIDATOR_NAME = 'abstract_regexp';
/** @var string Override in children classes to set regexp to use for validation */
public const DEFAULT_REGEXP = '';
protected string $sRegExp;
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];
}
return [false, $this->sErrorMessage];
}
/**
* Returns the regular expression of the validator.
*
* @param boolean $bWithSlashes If true, surrounds $sRegExp with '/'. Used with preg_match & co
*
* @return string
*/
public function GetRegExp($bWithSlashes = false)
{
if ($bWithSlashes) {
$sRet = '/'.str_replace('/', '\\/', $this->sRegExp).'/';
} else {
$sRet = $this->sRegExp;
}
return $sRet;
}
}

View File

@@ -0,0 +1,58 @@
<?php
/*
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Form\Validator;
use utils;
/**
* @since 3.1.0 N°6414 Field Validators refactoring
*/
abstract class AbstractValidator
{
public const VALIDATOR_NAME = 'abstract';
/** @var string Default message / dict key when an error occurs, if no custom one is specified in the constructor */
public const DEFAULT_ERROR_MESSAGE = 'Core:Validator:Default';
/** @var string message / dict key to use when an error occurs */
protected string $sErrorMessage;
public function __construct(?string $sErrorMessage)
{
if (false === utils::IsNullOrEmptyString($sErrorMessage)) {
$this->sErrorMessage = $sErrorMessage;
} else {
$this->sErrorMessage = self::DEFAULT_ERROR_MESSAGE;
}
}
/**
* @param mixed $value
*
* @return array<bool,?string> boolean valid for valid / invalid, and error message if invalid
*/
abstract public function Validate($value): array;
/**
* Name to use for JS counterparts
*
* @return string
*/
public static function GetName()
{
return static::VALIDATOR_NAME;
}
/**
* Still used in \Combodo\iTop\Renderer\Console\FieldRenderer\ConsoleSelectObjectFieldRenderer::Render :(
*
* @return string
*/
public function GetErrorMessage()
{
return $this->sErrorMessage;
}
}

View File

@@ -0,0 +1,22 @@
<?php
/*
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Form\Validator;
/**
* @since 3.1.0 N°6414
*/
class CustomRegexpValidator extends AbstractRegexpValidator
{
public const VALIDATOR_NAME = 'custom_regexp';
public function __construct(string $sRegExp, ?string $sErrorMessage = null)
{
parent::__construct($sErrorMessage);
$this->sRegExp = $sRegExp; // must be done after parent constructor call !
}
}

View File

@@ -24,10 +24,9 @@ namespace Combodo\iTop\Form\Validator;
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class IntegerValidator extends Validator
class IntegerValidator extends AbstractRegexpValidator
{
const VALIDATOR_NAME = 'integer';
const DEFAULT_REGEXP = '^[0-9]+$';
const DEFAULT_ERROR_MESSAGE = 'Core:Validator:MustBeInteger';
public const VALIDATOR_NAME = 'integer';
public const DEFAULT_REGEXP = '^[0-9]+$';
public const DEFAULT_ERROR_MESSAGE = 'Core:Validator:MustBeInteger';
}

View File

@@ -24,13 +24,7 @@ namespace Combodo\iTop\Form\Validator;
*
* @since 3.1
*/
class LinkedSetValidator extends Validator
class LinkedSetValidator extends AbstractRegexpValidator
{
const VALIDATOR_NAME = 'LinkedSetValidator';
/** @inheritdoc */
public static function GetName()
{
return static::VALIDATOR_NAME;
}
public const VALIDATOR_NAME = 'LinkedSetValidator';
}

View File

@@ -26,10 +26,10 @@ namespace Combodo\iTop\Form\Validator;
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class MandatoryValidator extends Validator
class MandatoryValidator extends AbstractRegexpValidator
{
const VALIDATOR_NAME = 'mandatory';
const DEFAULT_REGEXP = '.*\S.*';
const DEFAULT_ERROR_MESSAGE = 'Core:Validator:Mandatory';
public const VALIDATOR_NAME = 'mandatory';
public const DEFAULT_REGEXP = '.*\S.*';
public const DEFAULT_ERROR_MESSAGE = 'Core:Validator:Mandatory';
}

View File

@@ -24,10 +24,9 @@ namespace Combodo\iTop\Form\Validator;
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class NotEmptyExtKeyValidator extends Validator
class NotEmptyExtKeyValidator extends MandatoryValidator
{
const VALIDATOR_NAME = 'notemptyextkey';
const DEFAULT_REGEXP = '^[0-9]*[1-9][0-9]*$';
const DEFAULT_ERROR_MESSAGE = 'Core:Validator:MustSelectOne';
public const VALIDATOR_NAME = 'notemptyextkey';
public const DEFAULT_REGEXP = '^[0-9]*[1-9][0-9]*$';
public const DEFAULT_ERROR_MESSAGE = 'Core:Validator:MustSelectOne';
}

View File

@@ -19,99 +19,22 @@
namespace Combodo\iTop\Form\Validator;
use DeprecatedCallsLog;
/**
* Description of Validator
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @deprecated 3.1.0 N°6414 use {@see \Combodo\iTop\Form\Validator\CustomRegexpValidator} instead
*/
class Validator
class Validator extends CustomRegexpValidator
{
const VALIDATOR_NAME = 'expression';
const DEFAULT_REGEXP = '';
const DEFAULT_ERROR_MESSAGE = 'Core:Validator:Default';
protected $sRegExp;
protected $sErrorMessage;
public static function GetName()
{
return static::VALIDATOR_NAME;
}
/**
*
* @param string $sRegExp
* @param string $sErrorMessage
*/
public function __construct($sRegExp = null, $sErrorMessage = null)
{
$this->sRegExp = ($sRegExp === null) ? static::DEFAULT_REGEXP : $sRegExp;
$this->sErrorMessage = ($sErrorMessage === null) ? static::DEFAULT_ERROR_MESSAGE : $sErrorMessage;
$this->ComputeConstraints();
}
// cannot use DeprecatedCallsLog::NotifyDeprecatedFile as it would trigger an exception on dev env
// because all autoloader files are loaded during MetaModel::Startup (calling \Combodo\iTop\Service\Events\EventService::InitService calling \utils::GetClassesForInterface)
DeprecatedCallsLog::NotifyDeprecatedPhpMethod('3.1.0 N°6414 use '.CustomRegexpValidator::class.' instead');
/**
* Returns the regular expression of the validator.
*
* @param boolean $bWithSlashes If true, surrounds $sRegExp with '/'. Used with preg_match & co
* @return string
*/
public function GetRegExp($bWithSlashes = false)
{
if ($bWithSlashes)
{
$sRet = '/' . str_replace('/', '\\/', $this->sRegExp) . '/';
}
else
{
$sRet = $this->sRegExp;
}
return $sRet;
parent::__construct($sRegExp, $sErrorMessage);
}
public function GetErrorMessage()
{
return $this->sErrorMessage;
}
public function SetRegExp($sRegExp)
{
$this->sRegExp = $sRegExp;
$this->ComputeConstraints();
return $this;
}
public function SetErrorMessage($sErrorMessage)
{
$this->sErrorMessage = $sErrorMessage;
$this->ComputeConstraints();
return $this;
}
/**
* Computes the regular expression and error message when changing constraints on the validator.
* Should be called in the validator's setters.
*/
public function ComputeConstraints()
{
$this->ComputeRegularExpression();
$this->ComputeErrorMessage();
}
/**
* Computes the regular expression when changing constraints on the validator.
*/
public function ComputeRegularExpression()
{
// Overload when necessary
}
/**
* Computes the error message when changing constraints on the validator.
*/
public function ComputeErrorMessage()
{
// Overload when necessary
}
}