Required.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Required elements validation
  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: Required.php,v 1.6 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. * Required elements validation
  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_Required extends HTML_QuickForm_Rule
  36. {
  37. /**
  38. * Checks if an element is empty
  39. *
  40. * @param string $value Value to check
  41. * @param mixed $options Not used yet
  42. * @access public
  43. * @return boolean true if value is not empty
  44. */
  45. function validate($value, $options = null)
  46. {
  47. // It seems this is a file.
  48. if (is_array($value)) {
  49. if (isset($value['name']) &&
  50. isset($value['type']) &&
  51. isset($value['tmp_name']) &&
  52. isset($value['size']) &&
  53. isset($value['error'])
  54. ){
  55. if (empty($value['tmp_name'])) {
  56. return false;
  57. }
  58. }
  59. } else {
  60. if ((string)$value == '') {
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. function getValidationScript($options = null)
  67. {
  68. return array('', "{jsVar} == ''");
  69. }
  70. }