diff --git a/sources/Form/Field/MultipleChoicesField.php b/sources/Form/Field/MultipleChoicesField.php index 0e06a314a..20ab99749 100644 --- a/sources/Form/Field/MultipleChoicesField.php +++ b/sources/Form/Field/MultipleChoicesField.php @@ -20,6 +20,7 @@ namespace Combodo\iTop\Form\Field; use Closure; +use utils; /** * Description of MultipleChoicesField @@ -213,17 +214,25 @@ abstract class MultipleChoicesField extends Field /** * @inheritDoc */ - public function Validate() - { + public function Validate() { $this->SetValid(true); $this->EmptyErrorMessages(); - foreach ($this->GetValidators() as $oValidator) - { - foreach ($this->currentValue as $value) - { - if (!preg_match($oValidator->GetRegExp(true), $value)) - { + if (count($this->currentValue) > 0) { + foreach ($this->currentValue as $sCode => $value) { + if (utils::IsNullOrEmptyString($value)) { + continue; + } + 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->AddErrorMessage($oValidator->GetErrorMessage()); } diff --git a/sources/Form/Field/SelectObjectField.php b/sources/Form/Field/SelectObjectField.php index a0ae5207c..df547f977 100644 --- a/sources/Form/Field/SelectObjectField.php +++ b/sources/Form/Field/SelectObjectField.php @@ -28,6 +28,7 @@ use DBSearch; use FieldExpression; use MetaModel; use ScalarExpression; +use utils; /** * Description of SelectObjectField @@ -237,19 +238,40 @@ class SelectObjectField extends Field /** * @return int */ - public function GetControlType() - { + public function GetControlType() { return $this->iControlType; } /** * @return string|null */ - public function GetSearchEndpoint() - { + public function GetSearchEndpoint() { 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. * By default, reset is done ONLY when the field is not read-only.