PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/contentmanager/code/trunk/administrator/components/com_contentmanager/libraries/jxtended/form/validator.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 173 lines | 92 code | 24 blank | 57 comment | 17 complexity | 332d772ffaa65eab05cb9bf445f50084 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: validator.php 160 2009-07-09 00:06:09Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Form
  6. * @copyright Copyright (C) 2008 - 2009 JXtended, LLC. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://jxtended.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Form Validator class for JXtended Libraries.
  13. *
  14. * @package JXtended.Libraries
  15. * @subpackage Forms
  16. * @version 2.0
  17. */
  18. class JXFormValidator extends JObject
  19. {
  20. /**
  21. * Method to validate a group of fields.
  22. *
  23. * @access public
  24. * @param array $fields An array of fields to validate.
  25. * @param array $data The data to validate.
  26. * @return mixed Array on success, JException on error.
  27. * @since 2.0
  28. */
  29. function validate(&$fields, &$data)
  30. {
  31. $results = array();
  32. foreach ($fields as $name => $field)
  33. {
  34. // Get the data for the field.
  35. $value = array_key_exists($name, $data) ? $data[$name] : null;
  36. // Check if the field is required.
  37. if ($field->attributes('required') == 'true')
  38. {
  39. // Check if the field value is empty.
  40. if ($value === '')
  41. {
  42. // The required field is empty!
  43. if ($message = $field->attributes('message')) {
  44. $results[] = new JException($message, 0, E_WARNING);
  45. } else {
  46. $results[] = new JException(JText::sprintf('JX_LIBRARIES_FORM_VALIDATOR_FIELD_REQUIRED', JText::_($field->attributes('label'))), 0, E_WARNING);
  47. }
  48. // We don't want to display more than one message per field so continue to the next one.
  49. continue;
  50. }
  51. }
  52. // Run the field validator.
  53. $return = $this->_isValid($field, $value);
  54. // Check for an error.
  55. if (JError::isError($return)) {
  56. return $return;
  57. }
  58. // Check if the field is valid.
  59. if ($return === false)
  60. {
  61. // The field failed validation.
  62. if ($message = $field->attributes('message')) {
  63. $results[] = new JException($message, 0, E_WARNING);
  64. } else {
  65. $results[] = new JException(JText::sprintf('JX_LIBRARIES_FORM_VALIDATOR_FIELD_INVALID', $field->attributes('title')), 0, E_WARNING);
  66. }
  67. }
  68. }
  69. return $results;
  70. }
  71. /**
  72. * Method to test if a value is valid for a field.
  73. *
  74. * @access protected
  75. * @param object $field The field to validate.
  76. * @param mixed $value The value to validate.
  77. * @return mixed Boolean on success, JException on error.
  78. * @since 2.0
  79. */
  80. function _isValid(&$field, $value)
  81. {
  82. $result = true;
  83. // Get the validator type.
  84. if ($type = $field->attributes('validate'))
  85. {
  86. // Get the validator class.
  87. $class = 'JXFormValidate'.$type;
  88. if (!class_exists($class))
  89. {
  90. jimport('joomla.filesystem.path');
  91. // Attempt to load the rule file.
  92. if ($file = JPath::find(JXFormValidator::addRulePath(), $type.'.php')) {
  93. require_once $file;
  94. }
  95. if (!class_exists($class)) {
  96. return new JException(JText::sprintf('JX_LIBRARIES_FORM_VALIDATOR_RULE_NOT_FOUND', $type), 0, E_ERROR);
  97. }
  98. }
  99. // Run the validator.
  100. $rule = new $class;
  101. $result = $rule->test($field, $value);
  102. }
  103. return $result;
  104. }
  105. /**
  106. * Method to add a path to the list of rule include paths.
  107. *
  108. * @access public
  109. * @param mixed $new A path or array of paths to add.
  110. * @return array The list of paths that have been added.
  111. * @since 2.0
  112. * @static
  113. */
  114. function addRulePath($new = null)
  115. {
  116. static $paths;
  117. if (!isset($paths)) {
  118. $paths = array(dirname(__FILE__).DS.'rules');
  119. }
  120. // Force path to an array.
  121. settype($new, 'array');
  122. // Add the new paths to the list if not already there.
  123. foreach ($new as $path) {
  124. if (!in_array($path, $paths)) {
  125. array_unshift($paths, trim($path));
  126. }
  127. }
  128. return $paths;
  129. }
  130. }
  131. class JXFormValidatorRegex
  132. {
  133. var $_regex = null;
  134. function test(&$field, $value)
  135. {
  136. $result = true;
  137. if (empty($this->_regex)) {
  138. $result = new JException('Regex is invalid '.get_class($this));
  139. }
  140. else
  141. {
  142. $name = $field->attributes('name');
  143. if (!preg_match(chr(1).$this->_regex.chr(1), $value)) {
  144. $result = false;
  145. }
  146. }
  147. return $result;
  148. }
  149. }