Source for file FieldSetValidator.class.php

Documentation is available at FieldSetValidator.class.php

  1. <?php
  2.  
  3. //require_once(HARMONI."utilities/FieldSetValidator/FieldSetValidator.interface.php");
  4. require_once(HARMONI."utilities/FieldSetValidator/FieldSet.class.php");
  5. require_once(HARMONI."utilities/FieldSetValidator/RuleSet.class.php");
  6.  
  7. /**
  8. * the FieldSetValidator takes a FieldSet and a RuleSet and validates values between the two
  9. *
  10. * @package harmoni.utilities.fieldsetvalidator
  11. *
  12. * @copyright Copyright &copy; 2005, Middlebury College
  13. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  14. *
  15. * @version $Id: FieldSetValidator.class.php,v 1.5 2007/09/04 20:25:55 adamfranco Exp $
  16. */
  17. class FieldSetValidator {
  18. /**
  19. * the FieldSet object
  20. *
  21. * @access private
  22. * @var object FieldSet $_fieldset
  23. */
  24. var $_fieldset;
  25. /**
  26. * the RuleSet object
  27. *
  28. * @access private
  29. * @var object RuleSet $_ruleset
  30. */
  31. var $_ruleset;
  32. /**
  33. * the constructor
  34. *
  35. * @param object FieldSet $fieldset the FieldSet object to use for values
  36. * @param object RuleSet $ruleset the RuleSet object to use for rules/validation
  37. * @access public
  38. * @return void
  39. ***/
  40. function FieldSetValidator( $fieldset, $ruleset ) {
  41. $this->_fieldset = $fieldset;
  42. $this->_ruleset = $ruleset;
  43. }
  44. /**
  45. * sets the fieldset object to use to $fieldset
  46. *
  47. * @param object FieldSet $fieldset the object to use
  48. * @access public
  49. * @return void
  50. ***/
  51. function setFieldSet( $fieldset ) {
  52. $this->_fieldset = $fieldset;
  53. }
  54. /**
  55. * sets the ruleset object to use to $ruleset
  56. *
  57. * @param object RuleSet $ruleset the object to use
  58. * @access public
  59. * @return void
  60. ***/
  61. function setRuleSet( $ruleset ) {
  62. $this->_ruleset = $ruleset;
  63. }
  64. /**
  65. * validates the value of $key against the rules defined for it
  66. *
  67. * @param string $key the key to validate
  68. * @param optional boolean $throwErrors Should we throw the specified errors if validation
  69. * fails or just return true/false. Default = TRUE.
  70. * @access public
  71. * @return boolean if the validation was successful or not
  72. ***/
  73. function validate( $key, $throwErrors=true ) {
  74. // get the value from the FieldSet
  75. $val = $this->_fieldset->get($key);
  76. // run the rules and return
  77. return $this->_ruleset->validate( $key, $val, $throwErrors );
  78. }
  79. /**
  80. * validates all defined keys in the FieldSet against those in the RuleSet
  81. *
  82. * @param optional boolean $throwErrors Should we throw the specified errors if validation
  83. * fails or just return true/false. Default = TRUE.
  84. * @access public
  85. * @return boolean if the validation was successful or not
  86. ***/
  87. function validateAll( $throwErrors=true ) {
  88. // get all the defined keys
  89. $keys = array_unique(array_merge($this->_fieldset->getKeys(),$this->_ruleset->getKeys()));
  90. $error = false; // assume no error
  91. // go through them and validate
  92. foreach ($keys as $key) {
  93. if (!$this->validate( $key, $throwErrors )) $error = true;
  94. }
  95. if ($error) return false;
  96. return true;
  97. }
  98. }
  99.  
  100. ?>

Documentation generated on Wed, 19 Sep 2007 10:23:08 -0400 by phpDocumentor 1.3.0RC3