PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, CPL-1.0
  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. <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."
  48. examples=
  49. '
  50. <!--- view code --->
  51. <cfoutput>
  52. ##errorMessageOn(objectName="user", property="email")##
  53. </cfoutput>
  54. '
  55. categories="view-helper,errors" chapters="form-helpers-and-showing-errors" functions="errorMessagesOn">
  56. <cfargument name="objectName" type="string" required="true" hint="The variable name of the object to display the error message for.">
  57. <cfargument name="property" type="string" required="true" hint="The name of the property to display the error message for.">
  58. <cfargument name="prependText" type="string" required="false" hint="String to prepend to the error message.">
  59. <cfargument name="appendText" type="string" required="false" hint="String to append to the error message.">
  60. <cfargument name="wrapperElement" type="string" required="false" hint="HTML element to wrap the error message in.">
  61. <cfargument name="class" type="string" required="false" hint="CSS class to set on the wrapper element.">
  62. <cfscript>
  63. var loc = {};
  64. $args(name="errorMessageOn", args=arguments);
  65. loc.object = $getObject(arguments.objectName);
  66. if (application.wheels.showErrorInformation && !IsObject(loc.object))
  67. $throw(type="Wheels.IncorrectArguments", message="The `#arguments.objectName#` variable is not an object.");
  68. loc.error = loc.object.errorsOn(arguments.property);
  69. loc.returnValue = "";
  70. if (!ArrayIsEmpty(loc.error))
  71. {
  72. loc.content = arguments.prependText & loc.error[1].message & arguments.appendText;
  73. loc.returnValue = $element(name=arguments.wrapperElement, skip="objectName,property,prependText,appendText,wrapperElement", content=loc.content, attributes=arguments);
  74. }
  75. </cfscript>
  76. <cfreturn loc.returnValue>
  77. </cffunction>