mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
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:
56
sources/Form/Validator/AbstractRegexpValidator.php
Normal file
56
sources/Form/Validator/AbstractRegexpValidator.php
Normal 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;
|
||||
}
|
||||
}
|
||||
58
sources/Form/Validator/AbstractValidator.php
Normal file
58
sources/Form/Validator/AbstractValidator.php
Normal 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;
|
||||
}
|
||||
}
|
||||
22
sources/Form/Validator/CustomRegexpValidator.php
Normal file
22
sources/Form/Validator/CustomRegexpValidator.php
Normal 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 !
|
||||
}
|
||||
}
|
||||
@@ -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';
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user