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:
Pierre Goiffon
2023-06-22 14:53:07 +02:00
parent 6606af71ff
commit d085f15b6d
15 changed files with 126 additions and 67 deletions

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

View File

@@ -19,15 +19,15 @@
namespace Combodo\iTop\Form\Field;
use utils;
use Dict;
use utils;
/**
* Description of BlobField
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class BlobField extends Field
class BlobField extends AbstractSimpleField
{
/** @var string */
protected $sDownloadUrl;

View File

@@ -19,13 +19,13 @@
namespace Combodo\iTop\Form\Field;
use Str;
use AttributeDuration;
use Str;
/**
* Description of StringField
*/
class DurationField extends Field
class DurationField extends AbstractSimpleField
{
/**

View File

@@ -366,26 +366,26 @@ abstract class Field
}
}
$this->bMandatory = $bMandatory;
$this->bMandatory = $bMandatory;
return $this;
}
return $this;
}
/**
* @return AbstractValidator
* @since 3.1.0 N°6414
*/
protected function GetMandatoryValidatorInstance(): AbstractValidator
{
return new MandatoryValidator();
}
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
*
/**
* 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
*/
@@ -583,29 +583,5 @@ abstract class Field
* @uses SetValid()
* @uses AddErrorMessage()
*/
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();
}
abstract public function Validate();
}

View File

@@ -26,7 +26,7 @@ use Closure;
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class FileUploadField extends Field
class FileUploadField extends AbstractSimpleField
{
/** @var bool DEFAULT_ALLOW_DELETE */
const DEFAULT_ALLOW_DELETE = true;

View File

@@ -30,7 +30,7 @@ use ormLinkSet;
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @since 2.3.0
*/
class LinkedSetField extends Field
class LinkedSetField extends AbstractSimpleField
{
/** @var bool DEFAULT_INDIRECT */
const DEFAULT_INDIRECT = false;

View File

@@ -32,7 +32,7 @@ use utils;
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @since 2.3.0
*/
abstract class MultipleChoicesField extends Field
abstract class MultipleChoicesField extends AbstractSimpleField
{
/** @var bool DEFAULT_MULTIPLE_VALUES_ENABLED */
const DEFAULT_MULTIPLE_VALUES_ENABLED = false;

View File

@@ -39,7 +39,7 @@ use utils;
* @author Romain Quetiez <romain.quetiez@combodo.com>
* @since 2.3.0
*/
class SelectObjectField extends Field
class SelectObjectField extends AbstractSimpleField
{
/** @var int CONTROL_SELECT */
const CONTROL_SELECT = 1;

View File

@@ -24,7 +24,7 @@ namespace Combodo\iTop\Form\Field;
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class SetField extends Field
class SetField extends AbstractSimpleField
{
}

View File

@@ -19,8 +19,8 @@
namespace Combodo\iTop\Form\Field;
use \Closure;
use \Combodo\iTop\Form\Form;
use Closure;
use Combodo\iTop\Form\Form;
/**
* Description of SubFormField
@@ -29,7 +29,7 @@ use \Combodo\iTop\Form\Form;
*/
class SubFormField extends Field
{
protected $oForm;
protected $oForm;
/**
* @inheritDoc
@@ -61,12 +61,12 @@ class SubFormField extends Field
return $this;
}
/**
* Checks the validators to see if the field's current value is valid.
* Then sets $bValid and $aErrorMessages.
*
* @return boolean
*/
/**
* Checks the validators to see if the field's current value is valid.
* Then sets $bValid and $aErrorMessages.
*
* @inheritDoc
*/
public function Validate()
{
return $this->oForm->Validate();
@@ -124,13 +124,13 @@ class SubFormField extends Field
* @param boolean $bMandatory
*/
public function SetMandatory(bool $bMandatory)
{
foreach ($this->oForm->GetFields() as $oField)
{
$oField->SetMandatory($bMandatory);
}
parent::SetMandatory($bMandatory);
}
{
foreach ($this->oForm->GetFields() as $oField) {
$oField->SetMandatory($bMandatory);
}
return parent::SetMandatory($bMandatory);
}
/**
* Sets the read-only flag on all the fields on the form
@@ -144,7 +144,7 @@ class SubFormField extends Field
$oField->SetReadOnly($bReadOnly);
$oField->SetMandatory(false);
}
parent::SetReadOnly($bReadOnly);
return parent::SetReadOnly($bReadOnly);
}
/**
@@ -158,7 +158,7 @@ class SubFormField extends Field
{
$oField->SetHidden($bHidden);
}
parent::SetHidden($bHidden);
return parent::SetHidden($bHidden);
}
/**

View File

@@ -24,7 +24,7 @@ namespace Combodo\iTop\Form\Field;
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
class TagSetField extends Field
class TagSetField extends AbstractSimpleField
{
}

View File

@@ -24,7 +24,7 @@ namespace Combodo\iTop\Form\Field;
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
*/
abstract class TextField extends Field
abstract class TextField extends AbstractSimpleField
{
}