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