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

/content/code/trunk/administrator/components/com_artofcontent/libraries/jxtended/form/formvalidator.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 145 lines | 73 code | 21 blank | 51 comment | 15 complexity | c852546168081ef2b269682ba42f4fc6 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: formvalidator.php 484 2010-12-20 23:40:27Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Form
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License
  8. * @link http://www.theartofjoomla.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. * @since 1.1
  17. */
  18. class JFormValidator extends JObject
  19. {
  20. /**
  21. * Method to validate a group of fields.
  22. *
  23. * @param array $fields An array of fields to validate.
  24. * @param array $data The data to validate.
  25. * @return mixed Array on success, JException on error.
  26. */
  27. public function validate(&$fields, &$data)
  28. {
  29. $results = array();
  30. foreach ($fields as $name => $field)
  31. {
  32. // Get the data for the field.
  33. $value = array_key_exists($name, $data) ? $data[$name] : null;
  34. // Check if the field is required.
  35. if ($field->attributes('required') == 'true')
  36. {
  37. // Check if the field value is empty.
  38. if ($value === '')
  39. {
  40. // The required field is empty!
  41. if ($message = $field->attributes('message')) {
  42. $results[] = new JException(JText::_($message), 0, E_WARNING);
  43. } else {
  44. $results[] = new JException(JText::sprintf('Libraries_Form_Validator_Field_Required', JText::_($field->attributes('name'))), 0, E_WARNING);
  45. }
  46. // We don't want to display more than one message per field so continue to the next one.
  47. continue;
  48. }
  49. }
  50. // Run the field validator.
  51. $return = $this->_isValid($field, $data);
  52. // Check for an error.
  53. if (JError::isError($return)) {
  54. return $return;
  55. }
  56. // Check if the field is valid.
  57. if ($return === false)
  58. {
  59. // The field failed validation.
  60. if ($message = $field->attributes('message')) {
  61. $results[] = new JException(JText::_($message), 0, E_WARNING);
  62. } else {
  63. $results[] = new JException(JText::sprintf('Libraries_Form_Validator_Field_Invalid', $field->attributes('name')), 0, E_WARNING);
  64. }
  65. }
  66. }
  67. return $results;
  68. }
  69. /**
  70. * Method to test if a value is valid for a field.
  71. *
  72. * @param object $field The field to validate.
  73. * @param array $values The values to validate.
  74. * @return mixed Boolean on success, JException on error.
  75. */
  76. protected function _isValid(&$field, $values)
  77. {
  78. $result = true;
  79. // Get the validator type.
  80. if ($type = $field->attributes('validate'))
  81. {
  82. // Get the validator class.
  83. $class = 'JFormRule'.$type;
  84. if (!class_exists($class))
  85. {
  86. jimport('joomla.filesystem.path');
  87. // Attempt to load the rule file.
  88. if ($file = JPath::find(JFormValidator::addRulePath(), $type.'.php')) {
  89. require_once $file;
  90. }
  91. if (!class_exists($class)) {
  92. return new JException(JText::sprintf('Libraries_Form_Validator_Rule_Not_Found', $type), 0, E_ERROR);
  93. }
  94. }
  95. // Run the validator.
  96. $rule = new $class;
  97. $result = $rule->test($field, $values);
  98. }
  99. return $result;
  100. }
  101. /**
  102. * Method to add a path to the list of rule include paths.
  103. *
  104. * @param mixed $new A path or array of paths to add.
  105. * @return array The list of paths that have been added.
  106. * @static
  107. */
  108. public function addRulePath($new = null)
  109. {
  110. static $paths;
  111. if (!isset($paths)) {
  112. $paths = array(dirname(__FILE__).'/rules');
  113. }
  114. // Force path to an array.
  115. settype($new, 'array');
  116. // Add the new paths to the list if not already there.
  117. foreach ($new as $path) {
  118. if (!in_array($path, $paths)) {
  119. array_unshift($paths, trim($path));
  120. }
  121. }
  122. return $paths;
  123. }
  124. }