PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/system/library/PEAR/HTML/QuickForm2/Rule/Required.php

https://bitbucket.org/spekkionu/passworddb
PHP | 113 lines | 21 code | 3 blank | 89 comment | 1 complexity | 1cc8aa83a6bc5824fde4a3a0dc642c86 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Rule for required elements
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2006-2012, Alexey Borzov <avb@php.net>,
  10. * Bertrand Mansion <golgote@mamasam.com>
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * The names of the authors may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category HTML
  38. * @package HTML_QuickForm2
  39. * @author Alexey Borzov <avb@php.net>
  40. * @author Bertrand Mansion <golgote@mamasam.com>
  41. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  42. * @version SVN: $Id: Required.php 323363 2012-02-19 15:09:07Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /**
  46. * Rule checking that the form field is not empty
  47. */
  48. require_once 'HTML/QuickForm2/Rule/Nonempty.php';
  49. /**
  50. * Rule for required elements
  51. *
  52. * The main difference from "nonempty" Rule is that
  53. * - elements to which this Rule is attached will be considered required
  54. * ({@link HTML_QuickForm2_Node::isRequired()} will return true for them) and
  55. * marked accordingly when outputting the form
  56. * - this Rule can only be added directly to the element and other Rules can
  57. * only be added to it via and_() method
  58. *
  59. * @category HTML
  60. * @package HTML_QuickForm2
  61. * @author Alexey Borzov <avb@php.net>
  62. * @author Bertrand Mansion <golgote@mamasam.com>
  63. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  64. * @version Release: 2.0.0
  65. * @link http://pear.php.net/package/HTML_QuickForm2
  66. */
  67. class HTML_QuickForm2_Rule_Required extends HTML_QuickForm2_Rule_Nonempty
  68. {
  69. /**
  70. * Disallows adding a rule to the chain with an "or" operator
  71. *
  72. * Required rules are different from all others because they affect the
  73. * visual representation of an element ("* denotes required field").
  74. * Therefore we cannot allow chaining other rules to these via or_(), since
  75. * this will effectively mean that the field is not required anymore and the
  76. * visual difference is bogus.
  77. *
  78. * @param HTML_QuickForm2_Rule $next
  79. *
  80. * @throws HTML_QuickForm2_Exception
  81. */
  82. public function or_(HTML_QuickForm2_Rule $next)
  83. {
  84. throw new HTML_QuickForm2_Exception(
  85. 'or_(): Cannot add a rule to "required" rule'
  86. );
  87. }
  88. /**
  89. * Sets the error message output by the rule
  90. *
  91. * Required rules cannot have an empty error message as that may allow
  92. * validation to succeed even if the element is empty, and that will make
  93. * visual difference ("* denotes required field") bogus.
  94. *
  95. * @param string $message Error message to display if validation fails
  96. *
  97. * @return HTML_QuickForm2_Rule
  98. * @throws HTML_QuickForm2_InvalidArgumentException
  99. */
  100. public function setMessage($message)
  101. {
  102. if (!strlen($message)) {
  103. throw new HTML_QuickForm2_InvalidArgumentException(
  104. '"required" rule cannot have an empty error message'
  105. );
  106. }
  107. return parent::setMessage($message);
  108. }
  109. }
  110. ?>