Skip to content

View Helpers

errorMessagesFor()

errorMessagesFor() — returns string

Available in: controller Category: Error Functions

Builds and returns a list (ul tag with a default class of error-messages) containing all the error messages for all the properties of the object. Returns an empty string if no errors exist.

NameTypeRequiredDefaultDescription
objectNamestringyes—The variable name of the object to display error messages for.
classstringnoerror-messagesCSS class to set on the ul element.
showDuplicatesbooleannotrueWhether or not to show duplicate error messages.
encodebooleannotrueUse this argument to decide whether the output of the function should be encoded in order to prevent Cross Site Scripting (XSS) attacks. Set it to true to encode all relevant output for the specific HTML element in question (e.g. tag content, attribute values, and URLs). For HTML elements that have both tag content and attribute values you can set this argument to attributes to only encode attribute values and not tag content.
includeAssociationsbooleannotrue
// 1. Display all error messages for a user object
#errorMessagesFor(objectName="user")#
// -> <ul class="error-messages"><li>Email is not a valid email address.</li><li>Username can't be blank.</li></ul>
// -> (empty string when the object has no errors)

// 2. Use a custom CSS class on the wrapping ul element
#errorMessagesFor(objectName="user", class="form-errors")#
// -> <ul class="form-errors"><li>Email is not a valid email address.</li></ul>

// 3. Suppress duplicate error messages
#errorMessagesFor(objectName="user", showDuplicates=false)#
// -> <ul class="error-messages"><li>can't be blank.</li></ul>
// (only one "can't be blank." entry even if multiple fields have the same message)

// 4. Include errors from associated objects as well
#errorMessagesFor(objectName="order", includeAssociations=true)#
// -> <ul class="error-messages"><li>Name can't be blank.</li><li>Line items quantity must be greater than zero.</li></ul>