Feature: #85070 - New ViewHelper f:form.hasErrors
See forge#85070
Description
A new condition ViewHelper
<f: tells whether a property
of the current form has validation errors:
<div class="{f:form.hasErrors(for: 'blog.title', then: 'has-error')}">
<f:form.textfield property="title" />
</div>
It extends the usual condition ViewHelper base class, so
<f: and
<f: work as well:
<f:form.hasErrors for="blog.title">
<f:then>The title has at least one validation error.</f:then>
<f:else>The title is fine.</f:else>
</f:form.hasErrors>
Validation results could already be read with
<f:, but only as a block: that ViewHelper
assigns a variable and renders its children, so it cannot be used inline
inside an attribute. Setting a CSS class on a wrapping element therefore meant
wrapping the markup in a block and nesting an
<f: inside it.
The second reason is less obvious.
\TYPO3\ offers
has, which also reports errors of sub properties, but that method
cannot be reached from a template: Fluid resolves
{results. to
get, which only returns the errors of that very node. A template
checking
{results. for
for="author"
therefore sees
nothing when the error sits on
author.. The new ViewHelper
evaluates
has and reports errors of sub properties as well.
The
for
argument is required and must be a non-empty property path. Any
other value raises an exception, so an unresolved or mistyped variable cannot
silently match the whole form.
Impact
Templates can mark up the surroundings of a form field without wrapping them in a block:
<div class="form-group {f:form.hasErrors(for: 'email', then: 'error')}">
<label>Email</label>
<f:form.textfield property="email" />
</div>
For the error objects themselves,
<f: remains the
ViewHelper to use. The error class of a single form field is still set by the
error argument of that field, which needs no extra ViewHelper.