mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
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:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user