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

/libs/HTML/QuickForm2/Rule/Nonempty.php

https://github.com/CodeYellowBV/piwik
PHP | 141 lines | 52 code | 6 blank | 83 comment | 15 complexity | 04addd2b94427be3954d807e289f05d0 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Rule checking that the field is not empty
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2006-2010, 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: Nonempty.php 299706 2010-05-24 18:32:37Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /**
  46. * Base class for HTML_QuickForm2 rules
  47. */
  48. // require_once 'HTML/QuickForm2/Rule.php';
  49. /**
  50. * Rule checking that the field is not empty
  51. *
  52. * Handles simple form fields, file uploads and Containers.
  53. *
  54. * When validating <select multiple> fields and Containers it may use an
  55. * optional configuration parameter for minimum number of nonempty values,
  56. * defaulting to 1. It can be passed either to
  57. * {@link HTML_QuickForm2_Rule::__construct() the Rule constructor} as local
  58. * configuration or to {@link HTML_QuickForm2_Factory::registerRule()} as
  59. * global one. As usual, global configuration overrides local.
  60. *
  61. * <code>
  62. * // Required rule is 'nonempty' with a bit of special handling
  63. * $login->addRule('required', 'Please provide your login');
  64. * $multiSelect->addRule('required', 'Please select at least two options', 2);
  65. * </code>
  66. *
  67. * @category HTML
  68. * @package HTML_QuickForm2
  69. * @author Alexey Borzov <avb@php.net>
  70. * @author Bertrand Mansion <golgote@mamasam.com>
  71. * @version Release: @package_version@
  72. */
  73. class HTML_QuickForm2_Rule_Nonempty extends HTML_QuickForm2_Rule
  74. {
  75. protected function validateOwner()
  76. {
  77. if ($this->owner instanceof HTML_QuickForm2_Container) {
  78. $nonempty = 0;
  79. foreach ($this->owner->getRecursiveIterator(RecursiveIteratorIterator::LEAVES_ONLY) as $child) {
  80. $rule = new self($child);
  81. if ($rule->validateOwner()) {
  82. $nonempty++;
  83. }
  84. }
  85. return $nonempty >= $this->getConfig();
  86. }
  87. $value = $this->owner->getValue();
  88. if ($this->owner instanceof HTML_QuickForm2_Element_InputFile) {
  89. return isset($value['error']) && (UPLOAD_ERR_OK == $value['error']);
  90. } elseif (is_array($value)) {
  91. return count(array_filter($value, 'strlen')) >= $this->getConfig();
  92. } else {
  93. return (bool)strlen($value);
  94. }
  95. }
  96. /**
  97. * Sets minimum number of nonempty values
  98. *
  99. * This is useful for multiple selects and Containers, will be ignored for
  100. * all other elements. Defaults to 1, thus multiple select will be
  101. * considered not empty if at least one option is selected, Container will
  102. * be considered not empty if at least one contained element is not empty.
  103. *
  104. * @param int Maximum allowed size
  105. * @return HTML_QuickForm2_Rule
  106. * @throws HTML_QuickForm2_InvalidArgumentException if a bogus size limit was provided
  107. */
  108. public function setConfig($config)
  109. {
  110. if (is_null($config)) {
  111. $config = 1;
  112. } elseif (1 > intval($config)) {
  113. throw new HTML_QuickForm2_InvalidArgumentException(
  114. 'Nonempty Rule accepts a positive count of nonempty values, ' .
  115. preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
  116. );
  117. }
  118. return parent::setConfig(intval($config));
  119. }
  120. protected function getJavascriptCallback()
  121. {
  122. $js = "function() {var value = " . $this->owner->getJavascriptValue() . ";";
  123. if (!$this->owner instanceof HTML_QuickForm2_Container) {
  124. $js .= " if (!value instanceof Array) { return value != ''; } else { " .
  125. "var valid = 0; for (var i = 0; i < value.length; i++) { " .
  126. "if ('' != value[i]) { valid++; } } return valid >= " . $this->getConfig() . "; } }";
  127. } else {
  128. $js .= " var values = value.getValues(); var valid = 0; " .
  129. "for (var i = 0; i < values.length; i++) { " .
  130. "if ('' != values[i]) { valid++; } } return valid >= " . $this->getConfig() . "; }";
  131. }
  132. return $js;
  133. }
  134. }
  135. ?>