PageRenderTime 204ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/erlyweb-0.6.2/doc/erlyweb_forms.html

http://perferl.googlecode.com/
HTML | 130 lines | 108 code | 22 blank | 0 comment | 0 complexity | 14b8b792fe2870eb167654c52d012284 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Module erlyweb_forms</title>
  5. <link rel="stylesheet" type="text/css" href="stylesheet.css">
  6. <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
  7. </script>
  8. <script type="text/javascript">
  9. _uacct = "UA-494105-3";
  10. urchinTracker();
  11. </script>
  12. </head>
  13. <body bgcolor="white">
  14. <h1>Module erlyweb_forms</h1>
  15. This module contains a few functions useful when working with
  16. HTML forms in ErlyWeb.
  17. <p>Copyright Š Yariv Sadan 2006-2007</p>
  18. <ul><li><a href="#description">Description</a></li><li><a href="#index">Function Index</a></li><li><a href="#functions">Function Details</a></li></ul>
  19. <p><b>Authors:</b> Yariv Sadan (<a href="mailto:yarivsblog@gmail.com"><tt>yarivsblog@gmail.com</tt></a>) [<em>web site:</em> <tt><a href="http://yarivsblog.com)" target="_top">http://yarivsblog.com)</a></tt>].</p>
  20. <h2><a name="description">Description</a></h2>This module contains a few functions useful when working with
  21. HTML forms in ErlyWeb.
  22. <h2><a name="index">Function Index</a></h2>
  23. <table width="100%" border="1"><tr><td valign="top"><a href="#to_recs-2">to_recs/2</a></td><td>to_recs/2 helps process POST requests containing fields that
  24. belong to multiple records from one or more ErlyDB models.</td></tr>
  25. <tr><td valign="top"><a href="#validate-3">validate/3</a></td><td>validate/3 helps validate the inputs of arbitary forms.</td></tr>
  26. <tr><td valign="top"><a href="#validate1-3">validate1/3</a></td><td>validate1/3 is similar to validate/3, but it expects the parameter
  27. list to match the field list both in the number of elements and in their
  28. order.</td></tr>
  29. <tr><td valign="top"><a href="#validate_rec-2">validate_rec/2</a></td><td>When a form has fields that correspond to the fields of an ErlyDB
  30. record, validate_rec/2 helps validate the values of the record's fields.</td></tr>
  31. </table>
  32. <h2><a name="functions">Function Details</a></h2>
  33. <h3><a name="to_recs-2">to_recs/2</a></h3>
  34. <p><tt>to_recs(A::<a href="#type-arg">arg()</a> | [{ParamName::string(), ParamVal::term()}], ModelDescs::[{Prefix::string(), Model::atom()}]) -&gt; [Record::tuple()]</tt></p>
  35. <p><p>to_recs/2 helps process POST requests containing fields that
  36. belong to multiple records from one or more ErlyDB models.</p>
  37. <p>This function is useful when <a href="erlydb_base.html#new_fields_from_strs-3"><code>erlydb_base:new_fields_from_strs/3</code></a>
  38. isn't sufficient because the latter is only designed to map POST
  39. parameters to the fields of a single record.</p>
  40. <p>This function expects each form field to be mapped to its corresponding
  41. record by being named with a unique prefix identifying
  42. the record to which the form field belongs.</p>
  43. For example, suppose you have to process an HTML form whose fields
  44. represent a house and 2 cars. The house's fields have the
  45. prefix "house_" and the cars' fields have the prefixes "car1_" and
  46. "car2_". The arg's POST parameters are
  47. <code>[{"house_rooms", "3"}, {"car1_year", "2007"}, {"car2_year", "2006"}]</code>.
  48. With such a setup, calling <code>to_recs(A, [{"house_", house}, {"car1_", car},
  49. {"car2_", car}])</code>
  50. returns the list <code>[House, Car1, Car2]</code>, where <code>house:rooms(House) == "3"</code>,
  51. <code>car:year(Car1) == "2007"</code> and <code>car:year(Car2) == "2006"</code>. All other
  52. fields are <code>undefined</code>.
  53. </p>
  54. <h3><a name="validate-3">validate/3</a></h3>
  55. <p><tt>validate(A::<a href="#type-arg">arg()</a> | <a href="#type-proplist">proplist()</a>, Fields::[string()], Fun::function()) -&gt; {Values::[term()], Errors::[term()]} | <a href="#type-exit">exit({missing_param, Field})</a></tt></p>
  56. <p><p>validate/3 helps validate the inputs of arbitary forms.
  57. It accepts a Yaws arg
  58. (or the arg's POST data in the form of a name-value property list), a
  59. list of parameter names to validate, and a validation function, and returns
  60. a tuple of the form {Values, Errors}.
  61. 'Values' contains the list of values for the checked parameters
  62. and 'Errors' is a list of errors returned from the validation function.
  63. If no validation errors occured, this list is empty.</p>
  64. <p>If the name of a field is missing from the arg's POST data, this function
  65. calls exit({missing_param, Name}).</p>
  66. <p>The validation function takes two parameters: the parameter name and
  67. its value, and it may return one of the following values:</p>
  68. <p>- <code>ok</code> means the parameter's value is valid</p>
  69. <p>- <code>{ok, Val}</code> means the parameter's value is valid, and it also lets you
  70. set the value inserted into 'Values' for this parameter.</p>
  71. <p>- <code>{error, Err}</code> indicates the parameter didn't validate. Err is inserted
  72. into 'Errors'.</p>
  73. <p>- <code>{error, Err, Val}</code> indicates the parameter didn't validate. Err is
  74. inserted into 'Errors' and Val is inserted into 'Values' instead of
  75. the parameter's original value.</p>
  76. For forms that modify or create ErlyDB records, it's generally more
  77. convenient to use <a href="#to_recs-2"><code>to_recs/2</code></a>.
  78. </p>
  79. <h3><a name="validate1-3">validate1/3</a></h3>
  80. <p><tt>validate1(Params::<a href="#type-proplist">proplist()</a> | <a href="#type-arg">arg()</a>, Fields::[string()], Fun::function()) -&gt; {Vals, Errs} | <a href="#type-exit">exit({missing_params, [string()]})</a> | <a href="#type-exit">exit({unexpected_params, <a href="#type-proplist">proplist()</a>})</a> | <a href="#type-exit">exit({unexpected_param, string()})</a></tt></p>
  81. <p>validate1/3 is similar to validate/3, but it expects the parameter
  82. list to match the field list both in the number of elements and in their
  83. order. validate1/3 is more efficient and is also stricter than validate/3.</p>
  84. <p><b>See also:</b> <a href="#validate-3">validate/3</a>.</p>
  85. <h3><a name="validate_rec-2">validate_rec/2</a></h3>
  86. <p><tt>validate_rec(Rec::<a href="#type-erlydb_record">erlydb_record()</a>, Fun::function()) -&gt; {Rec1::<a href="#type-erlydb_record">erlydb_record()</a>, Errs::[term()]}</tt></p>
  87. <p><p>When a form has fields that correspond to the fields of an ErlyDB
  88. record, validate_rec/2 helps validate the values of the record's fields.</p>
  89. <p>validate_rec/2 accepts an ErlyDB record and a validation function.
  90. It folds over all the fields of the record (obtained by calling
  91. <a href="erlydb_base.html#db_field_names-0"><code>erlydb_base:db_field_names/0</code></a>), calling the validation function
  92. with each field's existing value. The validation function's
  93. return value indicates if the field's value is valid,
  94. and it may also define the record field's final value.</p>
  95. <p>The result of validate_rec/2 is a tuple of the form <code>{Rec1, Errs}</code>, where
  96. the first element is the modified record and the second element is
  97. a list of errors accumulated by the calls to the validation function.</p>
  98. <p>The validation function takes 3 parameters: the field name (an atom),
  99. the current value (this can be any term, but it's usually a string,
  100. especially if the record came from <a href="#to_recs-2"><code>to_recs/2</code></a>), and the record
  101. after folding over all the previous fields. It returns
  102. <code>ok</code>, <code>{ok, NewVal}</code>, <code>{error, Err}</code>, or <code>{error, Err, NewVal}</code>.</p>
  103. validate_rec/2 is especially useful in conjunction with <a href="#to_recs-2"><code>to_recs/2</code></a>.
  104. A common pattern is to create the records for the submitted form using
  105. to_recs/2 and then validate their fields using validate_rec/2.
  106. </p>
  107. </body>
  108. </html>