/lib/pkp/classes/form/validation/FormValidator.inc.php

https://github.com/lib-uoguelph-ca/ocs · PHP · 173 lines · 69 code · 25 blank · 79 comment · 14 complexity · 9a42ccc5a71d0592ed197ab037c6f6a9 MD5 · raw file

  1. <?php
  2. /**
  3. * @defgroup form_validation
  4. */
  5. /**
  6. * @file classes/form/validation/FormValidator.inc.php
  7. *
  8. * Copyright (c) 2000-2012 John Willinsky
  9. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  10. *
  11. * @class FormValidator
  12. * @ingroup form_validation
  13. *
  14. * @brief Class to represent a form validation check.
  15. */
  16. // The two allowed states for the type field
  17. define('FORM_VALIDATOR_OPTIONAL_VALUE', 'optional');
  18. define('FORM_VALIDATOR_REQUIRED_VALUE', 'required');
  19. class FormValidator {
  20. /** @var Form The Form associated with the check */
  21. var $_form;
  22. /** @var string The name of the field */
  23. var $_field;
  24. /** @var string The type of check ("required" or "optional") */
  25. var $_type;
  26. /** @var string The error message associated with a validation failure */
  27. var $_message;
  28. /** @var Validator The validator used to validate the field */
  29. var $_validator;
  30. /**
  31. * Constructor.
  32. * @param $form Form the associated form
  33. * @param $field string the name of the associated field
  34. * @param $type string the type of check, either "required" or "optional"
  35. * @param $message string the error message for validation failures (i18n key)
  36. * @param $validator Validator the validator used to validate this form field (optional)
  37. */
  38. function FormValidator(&$form, $field, $type, $message, $validator = null) {
  39. $this->_form =& $form;
  40. $this->_field = $field;
  41. $this->_type = $type;
  42. $this->_message = $message;
  43. $this->_validator =& $validator;
  44. $form->cssValidation[$field] = array();
  45. if ($type == FORM_VALIDATOR_REQUIRED_VALUE) {
  46. array_push($form->cssValidation[$field], 'required');
  47. }
  48. }
  49. //
  50. // Setters and Getters
  51. //
  52. /**
  53. * Get the field associated with the check.
  54. * @return string
  55. */
  56. function getField() {
  57. return $this->_field;
  58. }
  59. /**
  60. * Get the error message associated with a failed validation check.
  61. * @return string
  62. */
  63. function getMessage() {
  64. return __($this->_message);
  65. }
  66. /**
  67. * Set the form associated with this check. Used only for PHP4
  68. * compatibility when instantiating without =& (which is deprecated).
  69. * SHOULD NOT BE USED otherwise.
  70. * See http://pkp.sfu.ca/wiki/index.php/Information_for_Developers#Use_of_.24this_in_the_constructor
  71. * for a full explanation.
  72. */
  73. function setForm(&$form) {
  74. $this->_form =& $form;
  75. }
  76. /**
  77. * Get the form associated with the check
  78. * @return Form
  79. */
  80. function &getForm() {
  81. return $this->_form;
  82. }
  83. /**
  84. * Get the validator associated with the check
  85. * @return Validator
  86. */
  87. function &getValidator() {
  88. return $this->_validator;
  89. }
  90. /**
  91. * Get the type of the validated field ('optional' or 'required')
  92. * @return string
  93. */
  94. function getType() {
  95. return $this->_type;
  96. }
  97. //
  98. // Public methods
  99. //
  100. /**
  101. * Check if field value is valid.
  102. * Default check is that field is either optional or not empty.
  103. * @return boolean
  104. */
  105. function isValid() {
  106. if ($this->isEmptyAndOptional()) return true;
  107. $validator =& $this->getValidator();
  108. if (is_null($validator)) {
  109. // Default check: field must not be empty.
  110. $fieldValue = $this->getFieldValue();
  111. if (is_scalar($fieldValue)) {
  112. return $fieldValue !== '';
  113. } else {
  114. return $fieldValue !== array();
  115. }
  116. } else {
  117. // Delegate to the validator for the field value check.
  118. return $validator->isValid($this->getFieldValue());
  119. }
  120. }
  121. //
  122. // Protected helper methods
  123. //
  124. /**
  125. * Get field value
  126. * @return mixed
  127. */
  128. function getFieldValue() {
  129. $form =& $this->getForm();
  130. $fieldValue = $form->getData($this->getField());
  131. if (is_null($fieldValue) || is_scalar($fieldValue)) $fieldValue = trim((string)$fieldValue);
  132. return $fieldValue;
  133. }
  134. /**
  135. * Check if field value is empty and optional.
  136. * @return boolean
  137. */
  138. function isEmptyAndOptional() {
  139. if ($this->getType() != FORM_VALIDATOR_OPTIONAL_VALUE) return false;
  140. $fieldValue = $this->getFieldValue();
  141. if (is_scalar($fieldValue)) {
  142. return $fieldValue == '';
  143. } else {
  144. return empty($fieldValue);
  145. }
  146. }
  147. }
  148. ?>