Files
iTop/sources/Form/Field/AbstractSimpleField.php
Pierre Goiffon d085f15b6d 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
2023-07-03 11:30:04 +02:00

61 lines
1.4 KiB
PHP

<?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);
}
}