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