Skip to content

View Helpers

errorMessageOn()

errorMessageOn() — returns string

Available in: controller Category: Error Functions

Returns the error message, if one exists, on the object’s property. If multiple error messages exist, the first one is returned. If no error exists, it returns an empty string.

NameTypeRequiredDefaultDescription
objectNamestringyesThe variable name of the object to display the error message for.
propertystringyesThe name of the property to display the error message for.
prependTextstringnoString to prepend to the error message.
appendTextstringnoString to append to the error message.
wrapperElementstringnospanHTML element to wrap the error message in.
classstringnoerror-messageCSS class to set on the wrapper element.
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.
Example 1 — Basic usage
<cfoutput>
#errorMessageOn(objectName="author", property="email")#
</cfoutput>

Displays the first error message for the email property of the author object.

Default wrapper is <span class="error-message">.

Example 2 — Custom wrapper and class
<cfoutput>
#errorMessageOn(
 objectName="author",
 property="email",
 wrapperElement="div",
 class="alert alert-danger"
)#
</cfoutput>

Wraps the error in a <div> instead of <span>.

Uses Bootstrap classes for styling.

Example 3 — Prepend or append text
<cfoutput>
#errorMessageOn(
 objectName="author",
 property="email",
 prependText="Error: ",
 appendText=" Please fix it."
)#
</cfoutput>

Prepends "Error: " and appends " Please fix it." around the actual error message.

Example 4 — With HTML encoding disabled
<cfoutput>
#errorMessageOn(
 objectName="author",
 property="email",
 encode=false
)#
</cfoutput>

Output is not encoded, which can be useful if you want to include HTML formatting inside the error message itself.