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