Fluid icon indicating copy to clipboard operation
Fluid copied to clipboard

f:assert ViewHelper

Open Kanti opened this issue 6 months ago • 2 comments

A new <f:assert> ViewHelper should be added to Fluid, which checks if a provided values type is matching with the given type. If the type dose not match, the ViewHelper will throw an Exception.

Arguments:

mixed $value (can be any value that is matched against the type)
string $type

See also the pattern-matching rfc.

Examples:

<f:assert value="{user.name}" type="string" />
<f:assert value="{user.age}" type="int" />
<f:assert value="{user.birthday}" type="DateTimeInterface" />
<f:assert value="{data}" type="TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData" />
<f:assert value="{data}" type="?TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData" /> <!-- nullable -->
<f:assert value="{data}" type="TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData|null" /> <!-- nullable/union type -->
<f:assert value="{data}" type="string|array" /> <!-- union type -->
<f:assert value="{data}" type="Traversable&Countable" /><!-- intersection type -->
<f:assert value="{data}" type="User|(Account&Authenticated)" /><!-- union + intersection type -->
<f:assert value="{loginType}" type="0|100|200" /><!-- litteral + union type -->
<f:assert value="{loginType}" type="'login'|'logout'" /><!-- litteral + union type -->

Kanti avatar Jul 09 '25 12:07 Kanti

We would probably start with the types the ArgumentProcessor already supports.

s2b avatar Jul 09 '25 12:07 s2b

We just discussed this internally: We're currently unsure if this would cement the connection between the "PHP world" and Fluid templates too much. However, we're still open to the idea to add more validations to templates, so we will re-evaluate in the future.

s2b avatar Jul 28 '25 15:07 s2b