Back

errorMessageOn()

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.

Name Type Required Default Description
objectName string Yes The variable name of the object to display the error message for.
property string Yes The name of the property to display the error message for.
prependText string No String to prepend to the error message.
appendText string No String to append to the error message.
wrapperElement string No span HTML element to wrap the error message in.
class string No error-message CSS class to set on the wrapper element.
encode boolean No true Encode 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.
Copied!