PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 1ms

/wheels/view/forms.cfm

http://raihan.googlecode.com/
ColdFusion | 345 lines | 319 code | 24 blank | 2 comment | 60 complexity | e6965f440963f58a06975409b6450a34 MD5 | raw file
  1<cffunction name="endFormTag" returntype="string" access="public" output="false" hint="Builds and returns a string containing the closing `form` tag."
  2	examples=
  3	'
  4		<!--- view code --->
  5		<cfoutput>
  6		    ##startFormTag(action="create")##
  7		        <!--- your form controls --->
  8		    ##endFormTag()##
  9		</cfoutput>
 10	'
 11	categories="view-helper,forms-general" chapters="form-helpers-and-showing-errors" functions="URLFor,startFormTag,submitTag,textField,radioButton,checkBox,passwordField,hiddenField,textArea,fileField,select,dateTimeSelect,dateSelect,timeSelect">
 12	<cfscript>
 13		if (StructKeyExists(request.wheels, "currentFormMethod"))
 14			StructDelete(request.wheels, "currentFormMethod");
 15	</cfscript>
 16	<cfreturn "</form>">
 17</cffunction>
 18
 19<cffunction name="startFormTag" returntype="string" access="public" output="false" hint="Builds and returns a string containing the opening form tag. The form's action will be built according to the same rules as `URLFor`. Note: Pass any additional arguments like `class`, `rel`, and `id`, and the generated tag will also include those values as HTML attributes."
 20	examples=
 21	'
 22		<!--- view code --->
 23		<cfoutput>
 24		    ##startFormTag(action="create", spamProtection=true)##
 25		        <!--- your form controls --->
 26		    ##endFormTag()##
 27		</cfoutput>
 28	'
 29	categories="view-helper,forms-general" chapters="form-helpers-and-showing-errors" functions="URLFor,endFormTag,submitTag,textField,radioButton,checkBox,passwordField,hiddenField,textArea,fileField,select,dateTimeSelect,dateSelect,timeSelect">
 30	<cfargument name="method" type="string" required="false" hint="The type of method to use in the form tag. `get` and `post` are the options.">
 31	<cfargument name="multipart" type="boolean" required="false" hint="Set to `true` if the form should be able to upload files.">
 32	<cfargument name="spamProtection" type="boolean" required="false" hint="Set to `true` to protect the form against spammers (done with JavaScript).">
 33	<cfargument name="route" type="string" required="false" default="" hint="See documentation for @URLFor.">
 34	<cfargument name="controller" type="string" required="false" default="" hint="See documentation for @URLFor.">
 35	<cfargument name="action" type="string" required="false" default="" hint="See documentation for @URLFor.">
 36	<cfargument name="key" type="any" required="false" default="" hint="See documentation for @URLFor.">
 37	<cfargument name="params" type="string" required="false" default="" hint="See documentation for @URLFor.">
 38	<cfargument name="anchor" type="string" required="false" default="" hint="See documentation for @URLFor.">
 39	<cfargument name="onlyPath" type="boolean" required="false" hint="See documentation for @URLFor.">
 40	<cfargument name="host" type="string" required="false" hint="See documentation for @URLFor.">
 41	<cfargument name="protocol" type="string" required="false" hint="See documentation for @URLFor.">
 42	<cfargument name="port" type="numeric" required="false" hint="See documentation for @URLFor.">
 43	<cfscript>
 44		var loc = {};
 45		$args(name="startFormTag", args=arguments);
 46
 47		// sets a flag to indicate whether we use get or post on this form, used when obfuscating params
 48		request.wheels.currentFormMethod = arguments.method;
 49
 50		// set the form's action attribute to the URL that we want to send to
 51		if (!ReFindNoCase("^https?:\/\/", arguments.action))
 52			arguments.action = URLFor(argumentCollection=arguments);
 53
 54		// make sure we return XHMTL compliant code
 55		arguments.action = toXHTML(arguments.action);
 56
 57		// deletes the action attribute and instead adds some tricky javascript spam protection to the onsubmit attribute
 58		if (arguments.spamProtection)
 59		{
 60			loc.onsubmit = "this.action='#Left(arguments.action, int((Len(arguments.action)/2)))#'+'#Right(arguments.action, ceiling((Len(arguments.action)/2)))#';";
 61			arguments.onsubmit = $addToJavaScriptAttribute(name="onsubmit", content=loc.onsubmit, attributes=arguments);
 62			StructDelete(arguments, "action");
 63		}
 64
 65		// set the form to be able to handle file uploads
 66		if (!StructKeyExists(arguments, "enctype") && arguments.multipart)
 67			arguments.enctype = "multipart/form-data";
 68
 69		loc.skip = "multipart,spamProtection,route,controller,key,params,anchor,onlyPath,host,protocol,port";
 70		if (Len(arguments.route))
 71			loc.skip = ListAppend(loc.skip, $routeVariables(argumentCollection=arguments)); // variables passed in as route arguments should not be added to the html element
 72		if (ListFind(loc.skip, "action"))
 73			loc.skip = ListDeleteAt(loc.skip, ListFind(loc.skip, "action")); // need to re-add action here even if it was removed due to being a route variable above
 74
 75		loc.returnValue = $tag(name="form", skip=loc.skip, attributes=arguments);
 76	</cfscript>
 77	<cfreturn loc.returnValue>
 78</cffunction>
 79
 80<cffunction name="submitTag" returntype="string" access="public" output="false" hint="Builds and returns a string containing a submit button `form` control. Note: Pass any additional arguments like `class`, `rel`, and `id`, and the generated tag will also include those values as HTML attributes."
 81	examples=
 82	'
 83		!--- view code --->
 84		<cfoutput>
 85		    ##startFormTag(action="something")##
 86		        <!--- form controls go here --->
 87		        ##submitTag()##
 88		    ##endFormTag()##
 89		</cfoutput>
 90	'
 91	categories="view-helper,forms-general" chapters="form-helpers-and-showing-errors" functions="URLFor,startFormTag,endFormTag,textField,radioButton,checkBox,passwordField,hiddenField,textArea,fileField,select,dateTimeSelect,dateSelect,timeSelect">
 92	<cfargument name="value" type="string" required="false" hint="Message to display in the button form control.">
 93	<cfargument name="image" type="string" required="false" hint="File name of the image file to use in the button form control.">
 94	<cfargument name="disable" type="any" required="false" hint="Whether or not to disable the button upon clicking. (prevents double-clicking.)">
 95	<cfscript>
 96		var loc = {};
 97		$args(name="submitTag", reserved="type,src", args=arguments);
 98		if (Len(arguments.disable))
 99		{
100			loc.onclick = "this.disabled=true;";
101			if (!Len(arguments.image) && !IsBoolean(arguments.disable))
102				loc.onclick = loc.onclick & "this.value='#JSStringFormat(arguments.disable)#';";
103			loc.onclick = loc.onclick & "this.form.submit();";
104			arguments.onclick = $addToJavaScriptAttribute(name="onclick", content=loc.onclick, attributes=arguments);
105		}
106		if (Len(arguments.image))
107		{
108			// create an img tag and then just replace "img" with "input"
109			arguments.type = "image";
110			arguments.source = arguments.image;
111			StructDelete(arguments, "value");
112			StructDelete(arguments, "image");
113			StructDelete(arguments, "disable");
114			loc.returnValue = imageTag(argumentCollection=arguments);
115			loc.returnValue = Replace(loc.returnValue, "<img", "<input");
116		}
117		else
118		{
119			arguments.type = "submit";
120			loc.returnValue = $tag(name="input", close=true, skip="image,disable", attributes=arguments);
121		}
122	</cfscript>
123	<cfreturn loc.returnValue>
124</cffunction>
125
126<cffunction name="buttonTag" returntype="string" access="public" output="false" hint="Builds and returns a string containing a button `form` control."
127	examples=
128	'
129		<!--- view code --->
130		<cfoutput>
131		    ##startFormTag(action="something")##
132		        <!--- form controls go here --->
133		        ##buttonTag(content="Submit this form", value="save")##
134		    ##endFormTag()##
135		</cfoutput>
136	'
137	categories="view-helper,forms-general" chapters="form-helpers-and-showing-errors" functions="URLFor,startFormTag,endFormTag,textField,radioButton,checkBox,passwordField,hiddenField,textArea,fileField,select,dateTimeSelect,dateSelect,timeSelect">
138	<cfargument name="content" type="string" required="false" hint="Content to display inside the button.">
139	<cfargument name="type" type="string" required="false" hint="The type for the button: `button`, `reset`, or `submit`.">
140	<cfargument name="image" type="string" required="false" hint="File name of the image file to use in the button form control.">
141	<cfargument name="value" type="string" required="false" hint="The value of the button when submitted.">
142	<cfargument name="disable" type="any" required="false" hint="Whether or not to disable the button upon clicking. (Prevents double-clicking.)">
143	<cfscript>
144		var loc = {};
145		$args(name="buttonTag", args=arguments);
146
147		if (Len(arguments.disable))
148		{
149			loc.onclick = "this.disabled=true;";
150			if (!Len(arguments.image) && !IsBoolean(arguments.disable))
151				loc.onclick = loc.onclick & "this.value='#JSStringFormat(arguments.disable)#';";
152			loc.onclick = loc.onclick & "this.form.submit();";
153			arguments.onclick = $addToJavaScriptAttribute(name="onclick", content=loc.onclick, attributes=arguments);
154		}
155
156		if (Len(arguments.image))
157		{
158			// if image is specified then use that as the content
159			loc.args = {};
160			loc.args.type = "image";
161			loc.args.source = arguments.image;
162			arguments.content = imageTag(argumentCollection=loc.args);
163		}
164
165		// save content and delete argument
166		loc.content = arguments.content;
167		StructDelete(arguments, "content", false);
168		// remove image argument
169		StructDelete(arguments, "image");
170		// remove disabled argument
171		StructDelete(arguments, "disable");
172		// create the buttom
173		loc.returnValue = $element(name="button", content="#loc.content#", attributes="#arguments#");
174	</cfscript>
175	<cfreturn loc.returnValue>
176</cffunction>
177
178<cffunction name="$formValue" returntype="string" access="public" output="false">
179	<cfargument name="objectName" type="any" required="true">
180	<cfargument name="property" type="string" required="true">
181	<cfargument name="applyHtmlEditFormat" type="boolean" required="false" default="true" />
182	<cfscript>
183		var loc = {};
184		if (IsStruct(arguments.objectName))
185		{
186			loc.returnValue = arguments.objectName[arguments.property];
187		}
188		else
189		{
190			loc.object = $getObject(arguments.objectName);
191			if (application.wheels.showErrorInformation && !IsObject(loc.object))
192				$throw(type="Wheels.IncorrectArguments", message="The `#arguments.objectName#` variable is not an object.");
193			if (StructKeyExists(loc.object, arguments.property))
194				loc.returnValue = loc.object[arguments.property];
195			else
196				loc.returnValue = "";
197		}
198		if (arguments.applyHtmlEditFormat)
199			loc.returnValue = HTMLEditFormat(loc.returnValue);
200	</cfscript>
201	<cfreturn loc.returnValue>
202</cffunction>
203
204<cffunction name="$maxLength" returntype="any" access="public">
205	<cfargument name="objectName" type="any" required="true">
206	<cfargument name="property" type="string" required="true">
207	<cfscript>
208		var loc = {};
209
210		// if the developer passed in a maxlength value, use it
211		if (StructKeyExists(arguments, "maxlength"))
212			return arguments.maxlength;
213
214		// explicity return void so the property does not get set
215		if (IsStruct(arguments.objectName))
216			return;
217
218		loc.object = $getObject(arguments.objectName);
219
220		// if objectName does not represent an object, explicity return void so the property does not get set
221		if (not IsObject(loc.object))
222			return;
223
224		loc.propertyInfo = loc.object.$propertyInfo(arguments.property);
225		if (StructCount(loc.propertyInfo) and ListFindNoCase("cf_sql_char,cf_sql_varchar", loc.propertyInfo.type))
226			return loc.propertyInfo.size;
227	</cfscript>
228	<cfreturn />
229</cffunction>
230
231<cffunction name="$formHasError" returntype="boolean" access="public" output="false">
232	<cfargument name="objectName" type="any" required="true">
233	<cfargument name="property" type="string" required="true">
234	<cfscript>
235		var loc = {};
236		loc.returnValue = false;
237		if (!IsStruct(arguments.objectName))
238		{
239			loc.object = $getObject(arguments.objectName);
240			if (application.wheels.showErrorInformation && !IsObject(loc.object))
241				$throw(type="Wheels.IncorrectArguments", message="The `#arguments.objectName#` variable is not an object.");
242			if (ArrayLen(loc.object.errorsOn(arguments.property)))
243				loc.returnValue = true;
244		}
245	</cfscript>
246	<cfreturn loc.returnValue>
247</cffunction>
248
249<cffunction name="$createLabel" returntype="string" access="public" output="false">
250	<cfargument name="objectName" type="any" required="true">
251	<cfargument name="property" type="string" required="true">
252	<cfargument name="label" type="string" required="true">
253	<cfargument name="prependToLabel" type="string" required="true">
254	<cfscript>
255		var loc = {};
256		loc.returnValue = arguments.prependToLabel;
257		loc.attributes = {};
258		for (loc.key in arguments)
259			if (CompareNoCase(Left(loc.key, 5), "label") eq 0 && Len(loc.key) gt 5 && loc.key != "labelPlacement")
260				loc.attributes[ReplaceNoCase(loc.key, "label", "")] = arguments[loc.key];
261		if (StructKeyExists(arguments, "id"))
262			loc.attributes.for = arguments.id;
263		loc.returnValue = loc.returnValue & $tag(name="label", attributes=loc.attributes);
264		loc.returnValue = loc.returnValue & arguments.label;
265		loc.returnValue = loc.returnValue & "</label>";
266	</cfscript>
267	<cfreturn loc.returnValue>
268</cffunction>
269
270<cffunction name="$formBeforeElement" returntype="string" access="public" output="false">
271	<cfargument name="objectName" type="any" required="true">
272	<cfargument name="property" type="string" required="true">
273	<cfargument name="label" type="any" required="true">
274	<cfargument name="labelPlacement" type="string" required="true">
275	<cfargument name="prepend" type="string" required="true">
276	<cfargument name="append" type="string" required="true">
277	<cfargument name="prependToLabel" type="string" required="true">
278	<cfargument name="appendToLabel" type="string" required="true">
279	<cfargument name="errorElement" type="string" required="true">
280	<cfargument name="errorClass" type="string" required="true">
281	<cfscript>
282		var loc = {};
283		loc.returnValue = "";
284		if ($formHasError(argumentCollection=arguments) and Len(arguments.errorElement))
285			loc.returnValue = loc.returnValue & $tag(name=arguments.errorElement, class=arguments.errorClass);
286		arguments.label = $getFieldLabel(argumentCollection=arguments);
287		if (Len(arguments.label) && arguments.labelPlacement != "after")
288		{
289			loc.returnValue = loc.returnValue & $createLabel(argumentCollection=arguments);
290			if (arguments.labelPlacement == "around")
291				loc.returnValue = Replace(loc.returnValue, "</label>", "");
292			else
293				loc.returnValue = loc.returnValue & arguments.appendToLabel;
294
295		}
296		loc.returnValue = loc.returnValue & arguments.prepend;
297	</cfscript>
298	<cfreturn loc.returnValue>
299</cffunction>
300
301<cffunction name="$formAfterElement" returntype="string" access="public" output="false">
302	<cfargument name="objectName" type="any" required="true">
303	<cfargument name="property" type="string" required="true">
304	<cfargument name="label" type="string" required="true">
305	<cfargument name="labelPlacement" type="string" required="true">
306	<cfargument name="prepend" type="string" required="true">
307	<cfargument name="append" type="string" required="true">
308	<cfargument name="prependToLabel" type="string" required="true">
309	<cfargument name="appendToLabel" type="string" required="true">
310	<cfargument name="errorElement" type="string" required="true">
311	<cfscript>
312		var loc = {};
313		loc.returnValue = arguments.append;
314		arguments.label = $getFieldLabel(argumentCollection=arguments);
315		if (Len(arguments.label) && arguments.labelPlacement != "before")
316		{
317			if (arguments.labelPlacement == "after")
318				loc.returnValue = loc.returnValue & $createLabel(argumentCollection=arguments);
319			else if (arguments.labelPlacement == "around")
320				loc.returnValue = loc.returnValue & "</label>";
321			loc.returnValue = loc.returnValue & arguments.appendToLabel;
322		}
323		if ($formHasError(argumentCollection=arguments) and Len(arguments.errorElement))
324			loc.returnValue = loc.returnValue & "</" & arguments.errorElement & ">";
325	</cfscript>
326	<cfreturn loc.returnValue>
327</cffunction>
328
329<cffunction name="$getFieldLabel" returntype="string" access="public" output="false">
330	<cfargument name="objectName" type="any" required="true">
331	<cfargument name="property" type="string" required="true">
332	<cfargument name="label" type="string" required="true">
333	<cfscript>
334		var object = false;
335		if (Compare("false", arguments.label) == 0)
336			return "";
337		if (arguments.label == "useDefaultLabel" && !IsStruct(arguments.objectName))
338		{
339			object = $getObject(arguments.objectName);
340			if (IsObject(object))
341				return object.$label(arguments.property);
342		}
343	</cfscript>
344	<cfreturn arguments.label />
345</cffunction>