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
objectNamestringyesThe 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.
encodebooleannotrueEncode URL parameters using EncodeForURL(). Please note that this does not make the string safe for placement in HTML attributes, for that you need to wrap the result in EncodeForHtmlAttribute() or use linkTo(), startFormTag() etc instead.
includeAssociationsbooleannotrue
Example 1 — Basic usage
<cfoutput>
#errorMessagesFor(objectName="author")#
</cfoutput>

Generates a <ul class="error-messages"> containing all errors for the author object.

Default behavior includes all associated object errors.

Example 2 — Custom CSS class
<cfoutput>
#errorMessagesFor(objectName="author", class="alert alert-danger")#
</cfoutput>

Uses a custom CSS class for styling (e.g., Bootstrap alerts).

Example 3 — Exclude duplicate errors
<cfoutput>
#errorMessagesFor(objectName="author", showDuplicates=false)#
</cfoutput>

Prevents duplicate messages from appearing multiple times in the list.

Example 4 — Include or exclude associated objects
<cfoutput>
<!--- Only show errors on this object, not on associated objects --->
#errorMessagesFor(objectName="author", includeAssociations=false)#
</cfoutput>

Useful if you want to display errors for the main object separately from associated objects (like a nested profile or address).

Example 5 — HTML encoding disabled
<cfoutput>
#errorMessagesFor(objectName="author", encode=false)#
</cfoutput>

Errors are output as-is, allowing embedded HTML in the messages (use with caution).