PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/wheels/view/forms.cfm

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