PageRenderTime 354ms CodeModel.GetById 150ms app.highlight 2ms RepoModel.GetById 150ms app.codeStats 0ms

/wheels/view/errors.cfm

http://cfwheels.googlecode.com/
ColdFusion | 78 lines | 76 code | 1 blank | 1 comment | 8 complexity | 8edd6c1f97a6f6d853e9dd039f9d5e40 MD5 | raw file
 1<cffunction name="errorMessagesFor" returntype="string" access="public" output="false" hint="Builds and returns a list (`ul` tag with a class of `error-messages`) containing all the error messages for all the properties of the object (if any). Returns an empty string otherwise."
 2	examples=
 3	'
 4		<!--- view code --->
 5		<cfoutput>
 6		    ##errorMessagesFor(objectName="user")##
 7		</cfoutput>
 8	'
 9	categories="view-helper,errors" chapters="form-helpers-and-showing-errors" functions="errorMessagesOn">
10	<cfargument name="objectName" type="string" required="true" hint="The variable name of the object to display error messages for.">
11	<cfargument name="class" type="string" required="false" hint="CSS class to set on the `ul` element.">
12	<cfargument name="showDuplicates" type="boolean" required="false" hint="Whether or not to show duplicate error messages.">
13	<cfscript>
14		var loc = {};
15		$args(name="errorMessagesFor", args=arguments);
16		loc.object = $getObject(arguments.objectName);
17		if (application.wheels.showErrorInformation && !IsObject(loc.object))
18			$throw(type="Wheels.IncorrectArguments", message="The `#arguments.objectName#` variable is not an object.");
19		loc.errors = loc.object.allErrors();
20		loc.returnValue = "";
21		if (!ArrayIsEmpty(loc.errors))
22		{
23			loc.used = "";
24			loc.listItems = "";
25			loc.iEnd = ArrayLen(loc.errors);
26			for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
27			{
28				loc.msg = loc.errors[loc.i].message;
29				if(arguments.showDuplicates)
30				{
31					loc.listItems = loc.listItems & $element(name="li", content=loc.msg);
32				}
33				else
34				{
35					if(!ListFind(loc.used, loc.msg, Chr(7)))
36					{
37						loc.listItems = loc.listItems & $element(name="li", content=loc.msg);
38						loc.used = ListAppend(loc.used, loc.msg, Chr(7));
39					}
40				}
41			}
42			loc.returnValue = $element(name="ul", skip="objectName,showDuplicates", content=loc.listItems, attributes=arguments);
43		}
44	</cfscript>
45	<cfreturn loc.returnValue>
46</cffunction>
47
48<cffunction name="errorMessageOn" returntype="string" access="public" output="false" hint="Returns the error message, if one exists, on the object's property. If multiple error messages exist, the first one is returned."
49	examples=
50	'
51	<!--- view code --->
52	<cfoutput>
53	    ##errorMessageOn(objectName="user", property="email")##
54	</cfoutput>
55	'
56	categories="view-helper,errors" chapters="form-helpers-and-showing-errors" functions="errorMessagesOn">
57	<cfargument name="objectName" type="string" required="true" hint="The variable name of the object to display the error message for.">
58	<cfargument name="property" type="string" required="true" hint="The name of the property to display the error message for.">
59	<cfargument name="prependText" type="string" required="false" hint="String to prepend to the error message.">
60	<cfargument name="appendText" type="string" required="false" hint="String to append to the error message.">
61	<cfargument name="wrapperElement" type="string" required="false" hint="HTML element to wrap the error message in.">
62	<cfargument name="class" type="string" required="false" hint="CSS class to set on the wrapper element.">
63	<cfscript>
64		var loc = {};
65		$args(name="errorMessageOn", args=arguments);
66		loc.object = $getObject(arguments.objectName);
67		if (application.wheels.showErrorInformation && !IsObject(loc.object))
68			$throw(type="Wheels.IncorrectArguments", message="The `#arguments.objectName#` variable is not an object.");
69		loc.error = loc.object.errorsOn(arguments.property);
70		loc.returnValue = "";
71		if (!ArrayIsEmpty(loc.error))
72		{
73			loc.content = arguments.prependText & loc.error[1].message & arguments.appendText;
74			loc.returnValue = $element(name=arguments.wrapperElement, skip="objectName,property,prependText,appendText,wrapperElement", content=loc.content, attributes=arguments);
75		}
76	</cfscript>
77	<cfreturn loc.returnValue>
78</cffunction>