Required.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Required elements validation
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm
  15. * @author Bertrand Mansion <bmansion@mamasam.com>
  16. * @copyright 2001-2009 The PHP Group
  17. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  18. * @version CVS: $Id: Required.php,v 1.6 2009/04/04 21:34:04 avb Exp $
  19. * @link http://pear.php.net/package/HTML_QuickForm
  20. */
  21. /**
  22. * Required elements validation
  23. *
  24. * @category HTML
  25. * @package HTML_QuickForm
  26. * @author Bertrand Mansion <bmansion@mamasam.com>
  27. * @version Release: 3.2.11
  28. * @since 3.2
  29. */
  30. class HTML_QuickForm_Rule_Required extends HTML_QuickForm_Rule
  31. {
  32. /**
  33. * Checks if an element is empty
  34. *
  35. * @param string $value Value to check
  36. * @param mixed $options Not used yet
  37. * @access public
  38. * @return boolean true if value is not empty
  39. */
  40. public function validate($value, $options = null)
  41. {
  42. // It seems this is a file.
  43. if (is_array($value)) {
  44. if (isset($value['name']) &&
  45. isset($value['type']) &&
  46. isset($value['tmp_name']) &&
  47. isset($value['size']) &&
  48. isset($value['error'])
  49. ){
  50. if (empty($value['tmp_name'])) {
  51. return false;
  52. }
  53. }
  54. } else {
  55. if ((string)$value == '') {
  56. return false;
  57. }
  58. }
  59. return true;
  60. }
  61. public function getValidationScript($options = null)
  62. {
  63. return array('', "{jsVar} == ''");
  64. }
  65. }