PageRenderTime 62ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 156 lines | 105 code | 13 blank | 38 comment | 21 complexity | 8e75b6bb2967dd87769cd0806d117908 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: validate.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. * @package JXtended.Libraries
  13. * @subpackage Forms
  14. */
  15. class JXFormValidator extends JObject
  16. {
  17. /**
  18. * Form Descriptor
  19. * @access private
  20. * @var object
  21. */
  22. var $_descriptor = null;
  23. var $_input = null;
  24. var $_form = null;
  25. /**
  26. * Constructor
  27. *
  28. * @access protected
  29. * @param string $descriptor XML Form descriptor string
  30. * @param string $data INI Form value data string
  31. * @return void
  32. * @since 1.5
  33. */
  34. function __construct(&$descriptor, &$form)
  35. {
  36. if (is_a($descriptor, 'JSimpleXMLElement')) {
  37. $this->_descriptor =& $descriptor;
  38. }
  39. $this->_form = &$form;
  40. }
  41. /**
  42. * Validates an input array according to the rules in the descriptor
  43. * @param array
  44. * @return mixed True or JError
  45. */
  46. function validate(&$input)
  47. {
  48. $this->_input = &$input;
  49. $result = true;
  50. foreach ($this->_descriptor->children() as $field)
  51. {
  52. if ($name = $field->attributes('name'))
  53. {
  54. $value = $input->get($name);
  55. // First check if required attribute set
  56. if ($required = $field->attributes('required'))
  57. {
  58. if ($required == 'true')
  59. {
  60. if ($value === '')
  61. {
  62. // Failed
  63. if ($msg = $field->attributes('message')) {
  64. $result = new JException($msg);
  65. } else {
  66. $result = new JException($field->attributes('label').' required');
  67. }
  68. break;
  69. }
  70. }
  71. }
  72. $result = $this->_isValid($field);
  73. if (JError::isError($result)) {
  74. break;
  75. } else if ($result == false) {
  76. // Failed validation
  77. if ($msg = $field->attributes('message')) {
  78. $result = new JException($msg);
  79. } else {
  80. $result = new JException($field->attributes('label').' failed validation');
  81. }
  82. break;
  83. }
  84. }
  85. else
  86. {
  87. // Fatal error
  88. $result = new JException('Field parsing failed: name attribute required');
  89. break;
  90. }
  91. }
  92. return $result;
  93. }
  94. function &_isValid(&$field)
  95. {
  96. $result = true;
  97. if ($type = $field->attributes('validate'))
  98. {
  99. $class = 'JXFormValidate'.$type;
  100. if (!class_exists($class))
  101. {
  102. foreach (JXFormHelper::addIncludePath() as $path)
  103. {
  104. $path .= DS.'rules'.DS.$type.'.php';
  105. if (file_exists($path))
  106. {
  107. require_once $path;
  108. break;
  109. }
  110. }
  111. }
  112. if (class_exists($class)) {
  113. $rule = new $class;
  114. $result = $rule->test($field, $this->_input);
  115. } else {
  116. $result = new JException('Rule of type '.$type.' not available');
  117. }
  118. }
  119. return $result;
  120. }
  121. }
  122. /**
  123. * @abstract
  124. */
  125. class JXFormValidatorRegex
  126. {
  127. var $_regex = null;
  128. function test(&$field, &$values)
  129. {
  130. $result = true;
  131. if (empty($this->_regex)) {
  132. $result = new JException('Regex is invalid '.get_class($this));
  133. }
  134. else
  135. {
  136. $name = $field->attributes('name');
  137. if (!preg_match(chr(1).$this->_regex.chr(1), $values->get($name))) {
  138. $result = false;
  139. }
  140. }
  141. return $result;
  142. }
  143. }