API Docs for: 0.8.0
Show:

File: controllers/form_controller.js

  1. /*
  2. Copyright (C) 2015 PencilBlue, LLC
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17.  
  18. //dependencies
  19. var util = require('../include/util.js');
  20.  
  21. module.exports = function(pb) {
  22.  
  23. /**
  24. * Provides the basic functionality for implementing a controller that
  25. * needs access to a posted form.
  26. * @class FormController
  27. * @extends BaseController
  28. * @constructor
  29. */
  30. function FormController(){}
  31. util.inherits(FormController, pb.BaseController);
  32.  
  33. /**
  34. * Instructs the controller to automatically sanitize any incoming post data
  35. * when set to TRUE.
  36. * @property autoSanitize
  37. * @type {Boolean}
  38. */
  39. FormController.prototype.autoSanitize = true;
  40.  
  41. /**
  42. * Responsible for gathering the payload data from the request and parsing it.
  43. * The result is passed down to the controller's onPostParamsRetrieved function.
  44. * In addition and the <i>autoSanitize</i> property is TRUE, the posted
  45. * parameters will be sanitized.
  46. * @see BaseController#render
  47. * @method render
  48. * @param {Function} cb
  49. */
  50. FormController.prototype.render = function(cb) {
  51. var self = this;
  52. this.getPostParams(function(err, params) {
  53. if (util.isError(err)) {
  54. self.onPostParamsError(err, cb);
  55. return;
  56. }
  57.  
  58. if (self.getAutoSanitize()) {
  59. self.sanitizeObject(params);
  60. }
  61. self.onPostParamsRetrieved(params, cb);
  62. });
  63. };
  64.  
  65. /**
  66. *
  67. * @method getAutoSanitize
  68. * @return {Boolean}
  69. */
  70. FormController.prototype.getAutoSanitize = function() {
  71. return this.autoSanitize;
  72. };
  73.  
  74. /**
  75. *
  76. * @method setAutoSanitize
  77. * @param {Boolean} val
  78. */
  79. FormController.prototype.setAutoSanitize = function(val) {
  80. this.autoSanitize = !!val;
  81. };
  82.  
  83. /**
  84. *
  85. * @method onPostParamsError
  86. * @param {Error} err
  87. * @param {Function} cb
  88. */
  89. FormController.prototype.onPostParamsError = function(err, cb) {
  90. pb.log.silly("FormController: Error processing form parameters"+err);
  91. cb({content: err, code: 400});
  92. };
  93.  
  94. /**
  95. * Called after the posted parameters have been received and parsed. The
  96. * function should be overriden in order to continue processing and render the
  97. * result of the request. The default implementation echoes the received
  98. * parameters as JSON.
  99. * @method onPostParamsRetrieved
  100. * @param {Object} params
  101. * @param {Function} cb
  102. */
  103. FormController.prototype.onPostParamsRetrieved = function(params, cb) {
  104. cb({content: JSON.stringify(params), content_type:'application/json'});
  105. };
  106.  
  107. return FormController;
  108. };
  109.