PageRenderTime 61ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/core/helpers/validation/form.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 235 lines | 131 code | 24 blank | 80 comment | 17 complexity | 253037179cf2766cf1f1ca77f32d987d MD5 | raw file
  1. <?php
  2. /**
  3. * @package Helpers
  4. * @subpackage Validation
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. */
  9. /**
  10. * Helper functions to use with validating submitting forms.
  11. * @package Helpers
  12. * @subpackage Validation
  13. * @author Andrew Embler <andrew@concrete5.org>
  14. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  15. * @license http://www.concrete5.org/license/ MIT License
  16. */
  17. defined('C5_EXECUTE') or die("Access Denied.");
  18. class Concrete5_Helper_Validation_Form {
  19. protected $fields = array();
  20. protected $fieldsInvalid = array();
  21. protected $data = array();
  22. protected $files = array();
  23. protected $error;
  24. /**
  25. * @access private
  26. */
  27. const VALID_NOT_EMPTY = 1;
  28. const VALID_EMAIL = 2;
  29. const VALID_INTEGER = 3;
  30. const VALID_INTEGER_REQUIRED = 4;
  31. const VALID_UPLOADED_IMAGE = 10;
  32. const VALID_UPLOADED_IMAGE_REQUIRED = 11;
  33. const VALID_UPLOADED_FILE = 20;
  34. const VALID_UPLOADED_FILE_REQUIRED = 25;
  35. const VALID_TOKEN = 30;
  36. const VALID_FIELD_INVALID = 99;
  37. public function __construct() {
  38. $this->error = Loader::helper('validation/error');
  39. }
  40. /**
  41. * Adds a test to a field to ensure that, if set, it is a valid uploaded image.
  42. * @param string $field
  43. * @param string $errorMsg
  44. * @param bool $emptyIsOk Tells whether this can be submitted as empty (e.g. the validation tests only run if someone is actually submitted in the post.)
  45. * @return void
  46. */
  47. public function addUploadedImage($field, $errorMsg = null, $emptyIsOk = true) {
  48. $const = ($emptyIsOk) ? ValidationFormHelper::VALID_UPLOADED_IMAGE : ValidationFormHelper::VALID_UPLOADED_IMAGE_REQUIRED;
  49. $this->addRequired($field, $errorMsg, $const);
  50. }
  51. /**
  52. * Adds a test to a field to ensure that, if set, it is a valid uploaded file.
  53. * @param string $field
  54. * @param string $errorMsg
  55. * @param bool $emptyIsOk Tells whether this can be submitted as empty (e.g. the validation tests only run if someone is actually submitted in the post.)
  56. * @return void
  57. */
  58. public function addUploadedFile($field, $errorMsg = null, $emptyIsOk = true) {
  59. $const = ($emptyIsOk) ? ValidationFormHelper::VALID_UPLOADED_FILE : ValidationFormHelper::VALID_UPLOADED_FILE_REQUIRED;
  60. $this->addRequired($field, $errorMsg, $const);
  61. }
  62. /**
  63. * Adds a required field and tests that it is integer only
  64. * @param string $field
  65. * @param string $errorMsg
  66. * @param bool $emptyIsOk Tells whether this can be submitted as empty (e.g. the validation tests only run if someone is actually submitted in the post.)
  67. * @return void
  68. */
  69. public function addInteger($field, $errorMsg = null, $emptyIsOk = true) {
  70. $const = ($emptyIsOk) ? ValidationFormHelper::VALID_INTEGER : ValidationFormHelper::VALID_INTEGER_REQUIRED;
  71. $this->addRequired($field, $errorMsg, $const);
  72. }
  73. /**
  74. * Adds a required field to the form helper object. This will then be typically used in conjunction with the test() method to see
  75. * if the test is passed
  76. * @param string $field
  77. * @param string $errorMsg
  78. * @param string $validate test to validate against
  79. * @return void
  80. */
  81. public function addRequired($field, $errorMsg = null, $validate = ValidationFormHelper::VALID_NOT_EMPTY) {
  82. $obj = new stdClass;
  83. $obj->message = ($errorMsg == null) ? 'Field "' . $field . '" is invalid' : $errorMsg;
  84. $obj->field = $field;
  85. $obj->validate = $validate;
  86. $this->fields[] = $obj;
  87. }
  88. public function addRequiredToken($value, $errorMsg = null) {
  89. $obj = new stdClass;
  90. $vt = Loader::helper('validation/token');
  91. $obj->message = ($errorMsg == null) ? $vt->getErrorMessage() : $errorMsg;
  92. $obj->value = $value;
  93. $obj->validate = ValidationFormHelper::VALID_TOKEN;
  94. $this->fields[] = $obj;
  95. }
  96. /**
  97. * Adds a required email address to the suite of tests to be run.
  98. * @param string $field
  99. * @param string $errorMsg
  100. * @return void
  101. */
  102. public function addRequiredEmail($field, $errorMsg = null) {
  103. $this->addRequired($field, $errorMsg, ValidationFormHelper::VALID_EMAIL);
  104. }
  105. /**
  106. * Sets the data files array
  107. */
  108. public function setFiles() {
  109. $this->files = $_FILES;
  110. }
  111. /**
  112. * An associative array that we setup to validate against. Typical usage is $val->setData($_POST);
  113. * @param array $data
  114. * @return void
  115. */
  116. public function setData($data) {
  117. $this->data = $data;
  118. }
  119. /**
  120. * @access private
  121. */
  122. protected function setErrorsFromInvalidFields() {
  123. foreach($this->fieldsInvalid as $f) {
  124. $this->error->add($f->message);
  125. }
  126. }
  127. public function invalidate($message) {
  128. $f = new stdClass;
  129. $f->message = $message;
  130. $this->fieldsInvalid[] = $f;
  131. }
  132. /**
  133. * After the validation error helper has been setup, the test() method ensures that all fields that require validation
  134. * pass. Returns the number of invalid fields (0 = success)
  135. * @return int
  136. */
  137. public function test() {
  138. $val = Loader::helper('validation/strings');
  139. $num = Loader::helper('validation/numbers');
  140. $fil = Loader::helper('validation/file');
  141. // loop through all the fields in the array, and run whatever validation
  142. // the validate parameter says is required
  143. foreach($this->fields as $f) {
  144. $validate = $f->validate;
  145. $field = $f->field;
  146. switch($validate) {
  147. case ValidationFormHelper::VALID_NOT_EMPTY:
  148. if (!$val->notempty($this->data[$field])) {
  149. $this->fieldsInvalid[] = $f;
  150. }
  151. break;
  152. case ValidationFormHelper::VALID_TOKEN:
  153. $vt = Loader::helper('validation/token');
  154. if (!$vt->validate($f->value)) {
  155. $this->fieldsInvalid[] = $f;
  156. }
  157. break;
  158. case ValidationFormHelper::VALID_INTEGER:
  159. if ((!$num->integer($this->data[$field])) && ($val->notempty($this->data[$field]))) {
  160. $this->fieldsInvalid[] = $f;
  161. }
  162. break;
  163. case ValidationFormHelper::VALID_INTEGER_REQUIRED:
  164. if (!$num->integer($this->data[$field])) {
  165. $this->fieldsInvalid[] = $f;
  166. }
  167. break;
  168. case ValidationFormHelper::VALID_UPLOADED_IMAGE:
  169. if ((!$fil->image($this->files[$field]['tmp_name'])) && ($this->files[$field]['tmp_name'] != '')) {
  170. $this->fieldsInvalid[] = $f;
  171. }
  172. break;
  173. case ValidationFormHelper::VALID_UPLOADED_IMAGE_REQUIRED:
  174. if (!$fil->image($this->files[$field]['tmp_name'])) {
  175. $this->fieldsInvalid[] = $f;
  176. }
  177. break;
  178. case ValidationFormHelper::VALID_UPLOADED_FILE:
  179. if ((!$fil->file($this->files[$field]['tmp_name'])) && ($this->files[$field]['tmp_name'] != '')) {
  180. $this->fieldsInvalid[] = $f;
  181. }
  182. break;
  183. case ValidationFormHelper::VALID_UPLOADED_FILE_REQUIRED:
  184. if (!$fil->file($this->files[$field]['tmp_name'])) {
  185. $this->fieldsInvalid[] = $f;
  186. }
  187. break;
  188. case ValidationFormHelper::VALID_EMAIL:
  189. if (!$val->email($this->data[$field])) {
  190. $this->fieldsInvalid[] = $f;
  191. }
  192. break;
  193. }
  194. }
  195. $this->setErrorsFromInvalidFields();
  196. return count($this->fieldsInvalid) == 0;
  197. }
  198. /**
  199. * Gets the error object.
  200. * <code>
  201. * if ($val->test() > 0) {
  202. * $e = $val->getError();
  203. * }
  204. * </code>
  205. * @return ValidationErrorHelper
  206. */
  207. public function getError() {
  208. return $this->error;
  209. }
  210. }
  211. ?>