[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * TListControlValidator class file 4 * 5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TListControlValidator.php 1397 2006-09-07 07:55:53Z wei $ 10 * @package System.Web.UI.WebControls 11 */ 12 13 /** 14 * Using TBaseValidator class 15 */ 16 Prado::using('System.Web.UI.WebControls.TBaseValidator'); 17 18 /** 19 * TListControlValidator class. 20 * 21 * TListControlValidator checks the number of selection and their values 22 * for a <b>TListControl that allows multiple selection</b>. 23 * 24 * You can specify the minimum or maximum (or both) number of selections 25 * required using the {@link setMinSelection MinSelection} and 26 * {@link setMaxSelection MaxSelection} properties, respectively. In addition, 27 * you can specify a comma separated list of required selected values via the 28 * {@link setRequiredSelections RequiredSelections} property. 29 * 30 * Examples 31 * - At least two selections 32 * <code> 33 * <com:TListBox ID="listbox" SelectionMode="Multiple"> 34 * <com:TListItem Text="item1" Value="value1" /> 35 * <com:TListItem Text="item2" Value="value2" /> 36 * <com:TListItem Text="item3" Value="value3" /> 37 * </com:TListBox> 38 * 39 * <com:TListControlValidator 40 * ControlToValidate="listbox" 41 * MinSelection="2" 42 * ErrorMessage="Please select at least 2" /> 43 * </code> 44 * - "value1" must be selected <b>and</b> at least 1 other 45 * <code> 46 * <com:TCheckBoxList ID="checkboxes"> 47 * <com:TListItem Text="item1" Value="value1" /> 48 * <com:TListItem Text="item2" Value="value2" /> 49 * <com:TListItem Text="item3" Value="value3" /> 50 * </com:TCheckBoxList> 51 * 52 * <com:TListControlValidator 53 * ControlToValidate="checkboxes" 54 * RequiredSelections="value1" 55 * MinSelection="2" 56 * ErrorMessage="Please select 'item1' and at least 1 other" /> 57 * </code> 58 * 59 * @author Xiang Wei Zhuo <weizhuo[at]gmail.com> 60 * @version $Id: TListControlValidator.php 1397 2006-09-07 07:55:53Z wei $ 61 * @package System.Web.UI.WebControls 62 * @since 3.0 63 */ 64 class TListControlValidator extends TBaseValidator 65 { 66 /** 67 * Gets the name of the javascript class responsible for performing validation for this control. 68 * This method overrides the parent implementation. 69 * @return string the javascript class name 70 */ 71 protected function getClientClassName() 72 { 73 return 'Prado.WebUI.TListControlValidator'; 74 } 75 76 /** 77 * @return integer min number of selections. Defaults to -1, meaning not set. 78 */ 79 public function getMinSelection() 80 { 81 return $this->getViewState('MinSelection',-1); 82 } 83 84 /** 85 * @param integer minimum number of selections. 86 */ 87 public function setMinSelection($value) 88 { 89 if(($value=TPropertyValue::ensureInteger($value))<0) 90 $value=-1; 91 $this->setViewState('MinSelection',$value,-1); 92 } 93 94 /** 95 * @return integer max number of selections. Defaults to -1, meaning not set. 96 */ 97 public function getMaxSelection() 98 { 99 return $this->getViewState('MaxSelection',-1); 100 } 101 102 /** 103 * @param integer max number of selections. 104 */ 105 public function setMaxSelection($value) 106 { 107 if(($value=TPropertyValue::ensureInteger($value))<0) 108 $value=-1; 109 $this->setViewState('MaxSelection',$value,-1); 110 } 111 112 /** 113 * Get a comma separated list of required selected values. 114 * @return string comma separated list of required values. 115 */ 116 public function getRequiredSelections() 117 { 118 return $this->getViewState('RequiredSelections',''); 119 } 120 121 /** 122 * Set the list of required values, using aa comma separated list. 123 * @param string comma separated list of required values. 124 */ 125 public function setRequiredSelections($value) 126 { 127 $this->setViewState('RequiredSelections',$value,''); 128 } 129 130 /** 131 * This method overrides the parent's implementation. 132 * The validation succeeds if the input component changes its data 133 * from the InitialValue or the input component is not given. 134 * @return boolean whether the validation succeeds 135 */ 136 protected function evaluateIsValid() 137 { 138 $control=$this->getValidationTarget(); 139 140 $exists = true; 141 $values = $this->getSelection($control); 142 $count = count($values); 143 $required = $this->getRequiredValues(); 144 145 //if required, check the values 146 if(!empty($required)) 147 { 148 if($count < count($required) ) 149 return false; 150 foreach($required as $require) 151 $exists = $exists && in_array($require, $values); 152 } 153 154 $min = $this->getMinSelection(); 155 $max = $this->getMaxSelection(); 156 157 if($min !== -1 && $max !== -1) 158 return $exists && $count >= $min && $count <= $max; 159 else if($min === -1 && $max !== -1) 160 return $exists && $count <= $max; 161 else if($min !== -1 && $max === -1) 162 return $exists && $count >= $min; 163 else 164 return $exists; 165 } 166 167 /** 168 * @param TListControl control to validate 169 * @return array number of selected values and its values. 170 */ 171 protected function getSelection($control) 172 { 173 $values = array(); 174 175 //get the data 176 foreach($control->getItems() as $item) 177 { 178 if($item->getSelected()) 179 $values[] = $item->getValue(); 180 } 181 return $values; 182 } 183 184 /** 185 * @return array list of required values. 186 */ 187 protected function getRequiredValues() 188 { 189 $required = array(); 190 $string = $this->getRequiredSelections(); 191 if(!empty($string)) 192 $required = preg_split('/,\s*/', $string); 193 return $required; 194 } 195 196 /** 197 * Returns an array of javascript validator options. 198 * @return array javascript validator options. 199 */ 200 protected function getClientScriptOptions() 201 { 202 $options = parent::getClientScriptOptions(); 203 $control = $this->getValidationTarget(); 204 205 if(!$control instanceof TListControl) 206 { 207 throw new TConfigurationException( 208 'listcontrolvalidator_invalid_control', 209 $this->getID(),$this->getControlToValidate(), get_class($control)); 210 } 211 212 $min = $this->getMinSelection(); 213 $max = $this->getMaxSelection(); 214 if($min !== -1) 215 $options['Min']= $min; 216 if($max !== -1) 217 $options['Max']= $max; 218 $required = $this->getRequiredSelections(); 219 if(strlen($required) > 0) 220 $options['Required']= $required; 221 $options['TotalItems'] = $control->getItemCount(); 222 223 return $options; 224 } 225 } 226 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |