View Helpers
errorMessageOn()
Signature
Section titled “Signature”errorMessageOn() — returns string
Available in: controller
Category: Error Functions
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”| 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. |
Examples
Section titled “Examples”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.