N°1150 Add value checks in MultipleChoicesField and SelectObjectField

Now current value is checked against list of possible values
This commit is contained in:
Pierre Goiffon
2023-05-11 17:05:24 +02:00
parent 7c594db4b9
commit b71cd2182f
2 changed files with 43 additions and 12 deletions

View File

@@ -20,6 +20,7 @@
namespace Combodo\iTop\Form\Field; namespace Combodo\iTop\Form\Field;
use Closure; use Closure;
use utils;
/** /**
* Description of MultipleChoicesField * Description of MultipleChoicesField
@@ -213,17 +214,25 @@ abstract class MultipleChoicesField extends Field
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function Validate() public function Validate() {
{
$this->SetValid(true); $this->SetValid(true);
$this->EmptyErrorMessages(); $this->EmptyErrorMessages();
foreach ($this->GetValidators() as $oValidator) if (count($this->currentValue) > 0) {
{ foreach ($this->currentValue as $sCode => $value) {
foreach ($this->currentValue as $value) if (utils::IsNullOrEmptyString($value)) {
{ continue;
if (!preg_match($oValidator->GetRegExp(true), $value)) }
{ if (false === array_key_exists($value, $this->aChoices)) {
$this->SetValid(false);
$this->AddErrorMessage("Value ({$value}) is not part of the field possible values list");
}
}
}
foreach ($this->GetValidators() as $oValidator) {
foreach ($this->currentValue as $value) {
if (!preg_match($oValidator->GetRegExp(true), $value)) {
$this->SetValid(false); $this->SetValid(false);
$this->AddErrorMessage($oValidator->GetErrorMessage()); $this->AddErrorMessage($oValidator->GetErrorMessage());
} }

View File

@@ -28,6 +28,7 @@ use DBSearch;
use FieldExpression; use FieldExpression;
use MetaModel; use MetaModel;
use ScalarExpression; use ScalarExpression;
use utils;
/** /**
* Description of SelectObjectField * Description of SelectObjectField
@@ -237,19 +238,40 @@ class SelectObjectField extends Field
/** /**
* @return int * @return int
*/ */
public function GetControlType() public function GetControlType() {
{
return $this->iControlType; return $this->iControlType;
} }
/** /**
* @return string|null * @return string|null
*/ */
public function GetSearchEndpoint() public function GetSearchEndpoint() {
{
return $this->sSearchEndpoint; return $this->sSearchEndpoint;
} }
public function Validate() {
$sCurrentValueForExtKey = $this->currentValue;
if (utils::IsNotNullOrEmptyString($sCurrentValueForExtKey) && ($sCurrentValueForExtKey !== 0)) {
$oSearchForExistingCurrentValue = $this->oSearch->DeepClone();
$oSearchForExistingCurrentValue->AddCondition('id', $sCurrentValueForExtKey, '=');
$oCheckIdAgainstCurrentValueExpression = new BinaryExpression(
new FieldExpression('id', $oSearchForExistingCurrentValue->GetClassAlias()), '=', new ScalarExpression($sCurrentValueForExtKey)
);
$oSearchForExistingCurrentValue->AddConditionExpression($oCheckIdAgainstCurrentValueExpression);
$oSetForExistingCurrentValue = new DBObjectSet($oSearchForExistingCurrentValue);
$iObjectsCount = $oSetForExistingCurrentValue->CountWithLimit(1);
if ($iObjectsCount === 0) {
$this->SetValid(false);
$this->AddErrorMessage("Value $sCurrentValueForExtKey does not match the corresponding filter set");
return $this->GetValid();
}
}
return parent::Validate();
}
/** /**
* Resets current value if not among allowed ones. * Resets current value if not among allowed ones.
* By default, reset is done ONLY when the field is not read-only. * By default, reset is done ONLY when the field is not read-only.