N°6322 Delegate \AttributeCustomFields::CheckValue to the handler

This allows to move template_id checks from BuildForm (which is called in the UI to construct the form also) to a dedicated method only called for validation
This commit is contained in:
Pierre Goiffon
2023-05-23 18:09:50 +02:00
parent 63fcf4e6f9
commit 0508608e78
2 changed files with 36 additions and 27 deletions

View File

@@ -13245,27 +13245,11 @@ class AttributeCustomFields extends AttributeDefinition
*/
public function CheckValue(DBObject $oHostObject, $value)
{
try
{
try {
$oHandler = $this->GetHandler($value->GetValues());
$oHandler->BuildForm($oHostObject, '');
$oForm = $oHandler->GetForm();
$oForm->Validate();
if ($oForm->GetValid())
{
$ret = true;
}
else
{
$aMessages = array();
foreach($oForm->GetErrorMessages() as $sFieldId => $aFieldMessages)
{
$aMessages[] = $sFieldId.': '.implode(', ', $aFieldMessages);
}
$ret = 'Invalid value: '.implode(', ', $aMessages);
}
} catch (Exception $e)
{
$ret = $oHandler->Validate($oHostObject);
} catch (Exception $e) {
$ret = $e->getMessage();
}

View File

@@ -47,20 +47,47 @@ abstract class CustomFieldsHandler {
*
* @param $sAttCode
*/
final public function __construct($sAttCode)
{
final public function __construct($sAttCode) {
$this->sAttCode = $sAttCode;
$this->aValues = null;
}
abstract public function BuildForm(DBObject $oHostObject, $sFormId);
/**
* @returns true|string true if no error found, error message otherwise
* @throws \ApplicationException if {@link static::$oForm} attribute not initialized yet
* @since 3.1.0 N°6322 N°1150 Add template_id checks
*/
public function Validate(DBObject $oHostObject) {
if (false === isset($this->oForm)) {
throw new ApplicationException('oForm attribute not init yet. You must call BuildForm before this method !');
}
try {
$this->oForm->Validate();
if ($this->oForm->GetValid()) {
$ret = true;
}
else {
$aMessages = array();
foreach ($this->oForm->GetErrorMessages() as $sFieldId => $aFieldMessages) {
$aMessages[] = $sFieldId.': '.implode(', ', $aFieldMessages);
}
$ret = 'Invalid value: '.implode(', ', $aMessages);
}
} catch (Exception $e) {
$ret = $e->getMessage();
}
return $ret;
}
/**
*
* @return \Combodo\iTop\Form\Form
*/
public function GetForm()
{
public function GetForm() {
return $this->oForm;
}
@@ -69,16 +96,14 @@ abstract class CustomFieldsHandler {
$this->aValues = $aValues;
}
static public function GetPrerequisiteAttributes($sClass = null)
{
public static function GetPrerequisiteAttributes($sClass = null) {
return array();
}
/**
* List the available verbs for 'GetForTemplate'
*/
static public function EnumTemplateVerbs()
{
public static function EnumTemplateVerbs() {
return array();
}