Welcome to the next part of my series on error handling with Extbase and Fluid. This time I gonna show you how to place error fields next to the fields that cause them. First time ever I worked with Fluid I just found the ability to display errors as a list. For sure, that’s the easiest way arranging them because you just have to iterate through the error objects and echo the messages.
To be honest, I don’t really like the solution I am presenting you but it’s very convenient. As we are working with partials you should know what it is. If not, the next paragraph is for you, else head on to the next section.
A partial is nothing else but a part of code, most common a reusable one that represents some logic you need again and again. For instance an error list you put an error object in and once you defined how to process it you don’t care anymore in future projects. Just copy the partial, put in another object and the rest is pure magic. If you are used to object oriented programing, I guess you are, you can see the similarity.
Creating the Partial
It’s recommended that you put partials in a folder named “Partials” next to the templates folder. The full path is EXT:Resources/Private/Partials/.
Once you put in a partial, let’s say PartialName.html, you are able to render it inside your views with the use of the following Fluid render tag.
<f:render partial="PartialName" arguments="{arguments}" />
We will have a look at the arguments later, up to now its only necessary to know you can pass arguments in. So, here is the field error partial:
<!-- PartialName.html -->
<f:form.errors for="{objectName}">
<f:if condition="{0:error.propertyName} == {0:'{propertyName}'}">
<f:for each="{error.errors}" as="errorItem">
<f:translate key="error.{error.propertyName}.{errorItem.code}" htmlEscape="false">Not translated: [{errorItem.message} ({errorItem.code})]</f:translate>
</f:for>
</f:if>
</f:form.errors>
Similar to rendering a whole list we also iterate through all errors in this case except that we check if a certain error occurred. If so, we echo it via the translate tag I yet wrote about in my last post. I know, it’s a dirty way accessing a single error and I am curious about any other possible solution but I guess this one doesn’t take too much memory or CPU time.
Alright, we have to combine the partial with a fitting render tag now because we now need the arguments left out in my example above.
<f:render partial="FieldError" arguments="{objectName: 'newPerson', propertyName: 'email'}" />
The arguments object contains two properties called objectName and propertyName. Both are strings you just have to replace inside your project. In this example we want to create a new object named newPerson with an email property. These arguments are now available inside the partial and we are able to check whether the it’s an invalid email address. Most probably you place the render tag next to your input field but no matter where you put it, please do not build a list with it.
Any questions? Leave a reply.
Hello Alex,
thx for your code, it really helped! I wonder why the only time the error code gets displayed is with the Validator I wrote, the other ones dont give acces to that number.
Any Suggestions?
cheers miguel
Without any code I just can recommend you to debug the whole error object and look what’s actually wrong.
<f:form.errors><f:debug>{error.errors}</f:debug>
</f:form.errors>
Didn’ test that code but should work. If not try using just
{error}.