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

@@ -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.