mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01: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:
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Combodo\iTop\Form\Field;
|
||||
*
|
||||
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
|
||||
*/
|
||||
class SetField extends Field
|
||||
class SetField extends AbstractSimpleField
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Combodo\iTop\Form\Field;
|
||||
*
|
||||
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
|
||||
*/
|
||||
class TagSetField extends Field
|
||||
class TagSetField extends AbstractSimpleField
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user