Merge remote-tracking branch 'origin/feature/8772_form_dependencies_manager' into feature/8772_form_dependencies_manager

This commit is contained in:
Benjamin Dalsass
2025-11-14 14:32:56 +01:00
2 changed files with 30 additions and 6 deletions

View File

@@ -183,9 +183,25 @@ abstract class AbstractFormBlock implements IFormBlock
* @param string $sType the type of the input
*
* @return AbstractFormBlock
* @throws \Combodo\iTop\Forms\Block\FormBlockException
*/
public function AddInput(string $sName, string $sType): AbstractFormBlock
{
// Check name validity
if (preg_match('/(?<name>\w+)/', $sName, $aMatches)) {
$sParsedName = $aMatches['name'];
if ($sParsedName !== $sName) {
$sName = json_encode($sName);
$sParsedName = json_encode($sParsedName);
$sBlockName = json_encode($this->getName());
Throw new FormBlockException("Input $sName does not match $sParsedName for block $sBlockName.");
}
} else {
$sName = json_encode($sName);
$sBlockName = json_encode($this->getName());
Throw new FormBlockException("Input $sName is not valid for block $sBlockName.");
}
// Name is valid
$this->oIORegister->AddInput($sName, $sType);
return $this;
}

View File

@@ -13,13 +13,14 @@ use Combodo\iTop\Forms\Register\IORegister;
use Combodo\iTop\Forms\FormsException;
use IssueLog;
use Symfony\Component\Form\FormEvents;
use Throwable;
/**
*
*/
class ExpressionFormBlock extends AbstractFormBlock
{
public const EXPRESSION_PATTERN = "/\[\[(?<input>[^\]]+)]]/";
public const EXPRESSION_PATTERN = "/:(?<input>\w+)/";
// Outputs
const OUTPUT_RESULT = "result";
@@ -60,15 +61,22 @@ class ExpressionFormBlock extends AbstractFormBlock
if(!$oInput->HasEventValue($sEventType)){
throw new FormsException('Unable to compute expression: input '.$aMatches['input'].' has no value for event '.$sEventType.'.');
}
return $oInput->GetValue($sEventType);
$value = $oInput->GetValue($sEventType);
if (is_string($value)) {
$value = "'$value'";
}
return $value;
},
$sExpression);
$result = '';
eval('$result = '.$sValue.';');
$this->GetOutput(self::OUTPUT_RESULT)->SetValue($sEventType, new BooleanIOFormat($result));
$this->GetOutput(self::OUTPUT_RESULT_INVERT)->SetValue($sEventType, new BooleanIOFormat(!$result));
try {
eval('$result = '.$sValue.';');
} catch (Throwable $e) {
IssueLog::Exception(__METHOD__.' expression '.json_encode($sValue).' failed with exception: '.$e->getMessage(), $e);
}
$this->GetOutput(self::OUTPUT_RESULT)->SetValue($sEventType, new BooleanIOFormat(boolval($result)));
$this->GetOutput(self::OUTPUT_RESULT_INVERT)->SetValue($sEventType, new BooleanIOFormat(!boolval($result)));
$this->GetOutput(self::OUTPUT_VALUE)->SetValue($sEventType, new RawFormat($result));
}
catch(\Exception $e){