mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 02:28:44 +02: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:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user