From d085f15b6d8e03484975662961ebed4ab1c30582 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Thu, 22 Jun 2023 14:53:07 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B06414=20Separate=20SubFormField=20and=20S?= =?UTF-8?q?calar=20fields=20This=20will=20help=20to=20set=20methods=20as?= =?UTF-8?q?=20final,=20as=20SubFormField=20is=20often=20the=20sole=20one?= =?UTF-8?q?=20to=20have=20a=20real=20custom=20impl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/composer/autoload_classmap.php | 1 + lib/composer/autoload_static.php | 1 + sources/Form/Field/AbstractSimpleField.php | 61 +++++++++++++++++++ sources/Form/Field/BlobField.php | 4 +- sources/Form/Field/DurationField.php | 4 +- sources/Form/Field/Field.php | 52 +++++----------- sources/Form/Field/FileUploadField.php | 2 +- sources/Form/Field/LinkedSetField.php | 2 +- sources/Form/Field/MultipleChoicesField.php | 2 +- sources/Form/Field/SelectObjectField.php | 2 +- sources/Form/Field/SetField.php | 2 +- sources/Form/Field/SubFormField.php | 36 +++++------ sources/Form/Field/TagSetField.php | 2 +- sources/Form/Field/TextField.php | 2 +- .../sources/Form/Field/FieldTest.php | 20 ++++++ 15 files changed, 126 insertions(+), 67 deletions(-) create mode 100644 sources/Form/Field/AbstractSimpleField.php diff --git a/lib/composer/autoload_classmap.php b/lib/composer/autoload_classmap.php index 70da42bf4..d74eb09ed 100644 --- a/lib/composer/autoload_classmap.php +++ b/lib/composer/autoload_classmap.php @@ -386,6 +386,7 @@ return array( 'Combodo\\iTop\\Core\\MetaModel\\HierarchicalKey' => $baseDir . '/sources/Core/MetaModel/HierarchicalKey.php', 'Combodo\\iTop\\DesignDocument' => $baseDir . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\DesignElement' => $baseDir . '/core/designdocument.class.inc.php', + 'Combodo\\iTop\\Form\\Field\\AbstractSimpleField' => $baseDir . '/sources/Form/Field/AbstractSimpleField.php', 'Combodo\\iTop\\Form\\Field\\BlobField' => $baseDir . '/sources/Form/Field/BlobField.php', 'Combodo\\iTop\\Form\\Field\\CaseLogField' => $baseDir . '/sources/Form/Field/CaseLogField.php', 'Combodo\\iTop\\Form\\Field\\CheckboxField' => $baseDir . '/sources/Form/Field/CheckboxField.php', diff --git a/lib/composer/autoload_static.php b/lib/composer/autoload_static.php index 47275be29..b41cc4d5a 100644 --- a/lib/composer/autoload_static.php +++ b/lib/composer/autoload_static.php @@ -750,6 +750,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f 'Combodo\\iTop\\Core\\MetaModel\\HierarchicalKey' => __DIR__ . '/../..' . '/sources/Core/MetaModel/HierarchicalKey.php', 'Combodo\\iTop\\DesignDocument' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\DesignElement' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php', + 'Combodo\\iTop\\Form\\Field\\AbstractSimpleField' => __DIR__ . '/../..' . '/sources/Form/Field/AbstractSimpleField.php', 'Combodo\\iTop\\Form\\Field\\BlobField' => __DIR__ . '/../..' . '/sources/Form/Field/BlobField.php', 'Combodo\\iTop\\Form\\Field\\CaseLogField' => __DIR__ . '/../..' . '/sources/Form/Field/CaseLogField.php', 'Combodo\\iTop\\Form\\Field\\CheckboxField' => __DIR__ . '/../..' . '/sources/Form/Field/CheckboxField.php', diff --git a/sources/Form/Field/AbstractSimpleField.php b/sources/Form/Field/AbstractSimpleField.php new file mode 100644 index 000000000..65777bae3 --- /dev/null +++ b/sources/Form/Field/AbstractSimpleField.php @@ -0,0 +1,61 @@ +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); + } +} \ No newline at end of file diff --git a/sources/Form/Field/BlobField.php b/sources/Form/Field/BlobField.php index 210f55f11..871f95731 100644 --- a/sources/Form/Field/BlobField.php +++ b/sources/Form/Field/BlobField.php @@ -19,15 +19,15 @@ namespace Combodo\iTop\Form\Field; -use utils; use Dict; +use utils; /** * Description of BlobField * * @author Guillaume Lajarige */ -class BlobField extends Field +class BlobField extends AbstractSimpleField { /** @var string */ protected $sDownloadUrl; diff --git a/sources/Form/Field/DurationField.php b/sources/Form/Field/DurationField.php index 469aef11b..96eeb12be 100644 --- a/sources/Form/Field/DurationField.php +++ b/sources/Form/Field/DurationField.php @@ -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 { /** diff --git a/sources/Form/Field/Field.php b/sources/Form/Field/Field.php index 3e683c3d2..8bda28540 100644 --- a/sources/Form/Field/Field.php +++ b/sources/Form/Field/Field.php @@ -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(); } diff --git a/sources/Form/Field/FileUploadField.php b/sources/Form/Field/FileUploadField.php index 7d1c5b6b9..67074c6d3 100644 --- a/sources/Form/Field/FileUploadField.php +++ b/sources/Form/Field/FileUploadField.php @@ -26,7 +26,7 @@ use Closure; * * @author Guillaume Lajarige */ -class FileUploadField extends Field +class FileUploadField extends AbstractSimpleField { /** @var bool DEFAULT_ALLOW_DELETE */ const DEFAULT_ALLOW_DELETE = true; diff --git a/sources/Form/Field/LinkedSetField.php b/sources/Form/Field/LinkedSetField.php index 22d5f3349..14f6cdc9c 100644 --- a/sources/Form/Field/LinkedSetField.php +++ b/sources/Form/Field/LinkedSetField.php @@ -30,7 +30,7 @@ use ormLinkSet; * @author Guillaume Lajarige * @since 2.3.0 */ -class LinkedSetField extends Field +class LinkedSetField extends AbstractSimpleField { /** @var bool DEFAULT_INDIRECT */ const DEFAULT_INDIRECT = false; diff --git a/sources/Form/Field/MultipleChoicesField.php b/sources/Form/Field/MultipleChoicesField.php index f7791acb2..a86587bc1 100644 --- a/sources/Form/Field/MultipleChoicesField.php +++ b/sources/Form/Field/MultipleChoicesField.php @@ -32,7 +32,7 @@ use utils; * @author Guillaume Lajarige * @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; diff --git a/sources/Form/Field/SelectObjectField.php b/sources/Form/Field/SelectObjectField.php index 62aaadf5e..a2d3a60f4 100644 --- a/sources/Form/Field/SelectObjectField.php +++ b/sources/Form/Field/SelectObjectField.php @@ -39,7 +39,7 @@ use utils; * @author Romain Quetiez * @since 2.3.0 */ -class SelectObjectField extends Field +class SelectObjectField extends AbstractSimpleField { /** @var int CONTROL_SELECT */ const CONTROL_SELECT = 1; diff --git a/sources/Form/Field/SetField.php b/sources/Form/Field/SetField.php index 5174924a5..fa0e08ac0 100644 --- a/sources/Form/Field/SetField.php +++ b/sources/Form/Field/SetField.php @@ -24,7 +24,7 @@ namespace Combodo\iTop\Form\Field; * * @author Guillaume Lajarige */ -class SetField extends Field +class SetField extends AbstractSimpleField { } diff --git a/sources/Form/Field/SubFormField.php b/sources/Form/Field/SubFormField.php index 8437382ef..e22af10bc 100644 --- a/sources/Form/Field/SubFormField.php +++ b/sources/Form/Field/SubFormField.php @@ -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); } /** diff --git a/sources/Form/Field/TagSetField.php b/sources/Form/Field/TagSetField.php index 3eb3e7fcd..e45cc33ed 100644 --- a/sources/Form/Field/TagSetField.php +++ b/sources/Form/Field/TagSetField.php @@ -24,7 +24,7 @@ namespace Combodo\iTop\Form\Field; * * @author Guillaume Lajarige */ -class TagSetField extends Field +class TagSetField extends AbstractSimpleField { } diff --git a/sources/Form/Field/TextField.php b/sources/Form/Field/TextField.php index 62e10b3d5..334987439 100644 --- a/sources/Form/Field/TextField.php +++ b/sources/Form/Field/TextField.php @@ -24,7 +24,7 @@ namespace Combodo\iTop\Form\Field; * * @author Guillaume Lajarige */ -abstract class TextField extends Field +abstract class TextField extends AbstractSimpleField { } diff --git a/tests/php-unit-tests/unitary-tests/sources/Form/Field/FieldTest.php b/tests/php-unit-tests/unitary-tests/sources/Form/Field/FieldTest.php index 0654cf4bc..7bc1cd1f5 100644 --- a/tests/php-unit-tests/unitary-tests/sources/Form/Field/FieldTest.php +++ b/tests/php-unit-tests/unitary-tests/sources/Form/Field/FieldTest.php @@ -7,6 +7,7 @@ namespace Combodo\iTop\Test\UnitTest\Sources\Form\Field; use Combodo\iTop\Form\Field\StringField; +use Combodo\iTop\Form\Field\SubFormField; use Combodo\iTop\Form\Validator\CustomRegexpValidator; use Combodo\iTop\Form\Validator\IntegerValidator; use Combodo\iTop\Form\Validator\MandatoryValidator; @@ -80,4 +81,23 @@ class FieldTest extends ItopTestCase $this->assertCount(1, $oField->GetErrorMessages()); $this->assertSame($sFirstValidatorInvalidResultErrorMsg, $oField->GetErrorMessages()[0]); } + + public function testSubFormFieldValidation(): void + { + $oSubFormField = new SubFormField('test_subformfield'); + + $oField = new StringField('test_field'); + $oField->SetMandatory(true); + + $oSubFormField->GetForm()->AddField($oField); + + $bIsSubFormFieldValid = $oSubFormField->Validate(); + $this->assertFalse($bIsSubFormFieldValid); + $this->assertCount(1, $oSubFormField->GetErrorMessages()); + + $oField->SetCurrentValue('test string'); + $bIsSubFormFieldValidAfterFieldUpdate = $oSubFormField->Validate(); + $this->assertTrue($bIsSubFormFieldValidAfterFieldUpdate); + $this->assertCount(0, $oSubFormField->GetErrorMessages()); + } } \ No newline at end of file