diff --git a/sources/Forms/Block/AbstractFormBlock.php b/sources/Forms/Block/AbstractFormBlock.php index 37b8f32be..67e926fe8 100644 --- a/sources/Forms/Block/AbstractFormBlock.php +++ b/sources/Forms/Block/AbstractFormBlock.php @@ -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('/(?\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; } diff --git a/sources/Forms/Block/Expression/ExpressionFormBlock.php b/sources/Forms/Block/Expression/ExpressionFormBlock.php index 6a23c2804..4bc276fb7 100644 --- a/sources/Forms/Block/Expression/ExpressionFormBlock.php +++ b/sources/Forms/Block/Expression/ExpressionFormBlock.php @@ -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 = "/\[\[(?[^\]]+)]]/"; + public const EXPRESSION_PATTERN = "/:(?\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){