mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-30 14:08:46 +02:00
N°6414 Separate SubFormField and Scalar fields
This will help to set methods as final, as SubFormField is often the sole one to have a real custom impl
This commit is contained in:
61
sources/Form/Field/AbstractSimpleField.php
Normal file
61
sources/Form/Field/AbstractSimpleField.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/*
|
||||
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Form\Field;
|
||||
|
||||
use Combodo\iTop\Form\Validator\MandatoryValidator;
|
||||
|
||||
/**
|
||||
* @since 3.1.0 N°6414
|
||||
*/
|
||||
class AbstractSimpleField extends Field
|
||||
{
|
||||
public function Validate()
|
||||
{
|
||||
$this->SetValid(true);
|
||||
$this->EmptyErrorMessages();
|
||||
|
||||
if ($this->bValidationDisabled) {
|
||||
return $this->GetValid();
|
||||
}
|
||||
|
||||
$bEmpty = (($this->GetCurrentValue() === null) || ($this->GetCurrentValue() === ''));
|
||||
|
||||
if (!$bEmpty || $this->GetMandatory()) {
|
||||
foreach ($this->GetValidators() as $oValidator) {
|
||||
[$bIsFieldValid, $sValidationErrorMessage] = $oValidator->Validate($this->GetCurrentValue());
|
||||
|
||||
/** @var bool $bIsFieldValid */
|
||||
if (false === $bIsFieldValid) {
|
||||
$this->SetValid(false);
|
||||
$this->AddErrorMessage($sValidationErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->GetValid();
|
||||
}
|
||||
|
||||
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($this->GetMandatoryValidatorInstance());
|
||||
}
|
||||
|
||||
if (false === $bMandatory) {
|
||||
foreach ($this->aValidators as $iKey => $oValue) {
|
||||
if ($oValue instanceof MandatoryValidator) {
|
||||
unset($this->aValidators[$iKey]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->bMandatory = $bMandatory;
|
||||
|
||||
return parent::SetMandatory($bMandatory);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user