Callback.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Validates values using callback functions or methods
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @copyright 2001-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: Callback.php,v 1.9 2009/04/04 21:34:04 avb Exp $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * Abstract base class for QuickForm validation rules
  24. */
  25. require_once 'HTML/QuickForm/Rule.php';
  26. /**
  27. * Validates values using callback functions or methods
  28. *
  29. * @category HTML
  30. * @package HTML_QuickForm
  31. * @author Bertrand Mansion <bmansion@mamasam.com>
  32. * @version Release: 3.2.11
  33. * @since 3.2
  34. */
  35. class HTML_QuickForm_Rule_Callback extends HTML_QuickForm_Rule
  36. {
  37. /**
  38. * Array of callbacks
  39. *
  40. * Array is in the format:
  41. * $_data['rulename'] = array('functionname', 'classname');
  42. * If the callback is not a method, then the class name is not set.
  43. *
  44. * @var array
  45. * @access private
  46. */
  47. var $_data = array();
  48. /**
  49. * Whether to use BC mode for specific rules
  50. *
  51. * Previous versions of QF passed element's name as a first parameter
  52. * to validation functions, but not to validation methods. This behaviour
  53. * is emulated if you are using 'function' as rule type when registering.
  54. *
  55. * @var array
  56. * @access private
  57. */
  58. var $_BCMode = array();
  59. /**
  60. * Validates a value using a callback
  61. *
  62. * @param string $value Value to be checked
  63. * @param mixed $options Options for callback
  64. * @access public
  65. * @return boolean true if value is valid
  66. */
  67. function validate($value, $options = null)
  68. {
  69. if (isset($this->_data[$this->name])) {
  70. $callback = $this->_data[$this->name];
  71. if (isset($callback[1])) {
  72. return call_user_func(array($callback[1], $callback[0]), $value, $options);
  73. } elseif ($this->_BCMode[$this->name]) {
  74. return $callback[0]('', $value, $options);
  75. } else {
  76. return $callback[0]($value, $options);
  77. }
  78. } elseif (is_callable($options)) {
  79. return call_user_func($options, $value);
  80. } else {
  81. return true;
  82. }
  83. } // end func validate
  84. /**
  85. * Adds new callbacks to the callbacks list
  86. *
  87. * @param string $name Name of rule
  88. * @param string $callback Name of function or method
  89. * @param string $class Name of class containing the method
  90. * @param bool $BCMode Backwards compatibility mode
  91. * @access public
  92. */
  93. function addData($name, $callback, $class = null, $BCMode = false)
  94. {
  95. if (!empty($class)) {
  96. $this->_data[$name] = array($callback, $class);
  97. } else {
  98. $this->_data[$name] = array($callback);
  99. }
  100. $this->_BCMode[$name] = $BCMode;
  101. } // end func addData
  102. function getValidationScript($options = null)
  103. {
  104. if (isset($this->_data[$this->name])) {
  105. $callback = $this->_data[$this->name][0];
  106. $params = ($this->_BCMode[$this->name]? "'', {jsVar}": '{jsVar}') .
  107. (isset($options)? ", '{$options}'": '');
  108. } else {
  109. $callback = is_array($options)? $options[1]: $options;
  110. $params = '{jsVar}';
  111. }
  112. return array('', "{jsVar} != '' && !{$callback}({$params})");
  113. } // end func getValidationScript
  114. } // end class HTML_QuickForm_Rule_Callback
  115. ?>