Callback.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * Validates values using callback functions or methods
  24. *
  25. * @category HTML
  26. * @package HTML_QuickForm
  27. * @author Bertrand Mansion <bmansion@mamasam.com>
  28. * @version Release: 3.2.11
  29. * @since 3.2
  30. */
  31. class HTML_QuickForm_Rule_Callback extends HTML_QuickForm_Rule
  32. {
  33. /**
  34. * Array of callbacks
  35. *
  36. * Array is in the format:
  37. * $_data['rulename'] = array('functionname', 'classname');
  38. * If the callback is not a method, then the class name is not set.
  39. *
  40. * @var array
  41. * @access private
  42. */
  43. var $_data = array();
  44. /**
  45. * Whether to use BC mode for specific rules
  46. *
  47. * Previous versions of QF passed element's name as a first parameter
  48. * to validation functions, but not to validation methods. This behaviour
  49. * is emulated if you are using 'function' as rule type when registering.
  50. *
  51. * @var array
  52. * @access private
  53. */
  54. var $_BCMode = array();
  55. /**
  56. * Validates a value using a callback
  57. *
  58. * @param string $value Value to be checked
  59. * @param mixed $options Options for callback
  60. * @access public
  61. * @return boolean true if value is valid
  62. */
  63. function validate($value, $options = null)
  64. {
  65. if (isset($this->_data[$this->name])) {
  66. $callback = $this->_data[$this->name];
  67. if (isset($callback[1])) {
  68. return call_user_func(array($callback[1], $callback[0]), $value, $options);
  69. } elseif ($this->_BCMode[$this->name]) {
  70. return $callback[0]('', $value, $options);
  71. } else {
  72. return $callback[0]($value, $options);
  73. }
  74. } elseif (is_callable($options)) {
  75. return call_user_func($options, $value);
  76. } else {
  77. return true;
  78. }
  79. } // end func validate
  80. /**
  81. * Adds new callbacks to the callbacks list
  82. *
  83. * @param string $name Name of rule
  84. * @param string $callback Name of function or method
  85. * @param string $class Name of class containing the method
  86. * @param bool $BCMode Backwards compatibility mode
  87. * @access public
  88. */
  89. function addData($name, $callback, $class = null, $BCMode = false)
  90. {
  91. if (!empty($class)) {
  92. $this->_data[$name] = array($callback, $class);
  93. } else {
  94. $this->_data[$name] = array($callback);
  95. }
  96. $this->_BCMode[$name] = $BCMode;
  97. } // end func addData
  98. function getValidationScript($options = null)
  99. {
  100. if (isset($this->_data[$this->name])) {
  101. $callback = $this->_data[$this->name][0];
  102. $params = ($this->_BCMode[$this->name]? "'', {jsVar}": '{jsVar}') .
  103. (isset($options)? ", '{$options}'": '');
  104. } else {
  105. $callback = is_array($options)? $options[1]: $options;
  106. $params = '{jsVar}';
  107. }
  108. return array('', "{jsVar} != '' && !{$callback}({$params})");
  109. } // end func getValidationScript
  110. } // end class HTML_QuickForm_Rule_Callback
  111. ?>