3 * TListControlValidator class file
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id: TListControlValidator.php 3245 2013-01-07 20:23:32Z ctrlaltca $
10 * @package System.Web.UI.WebControls
14 * Using TBaseValidator class
16 Prado::using('System.Web.UI.WebControls.TBaseValidator');
19 * TListControlValidator class.
21 * TListControlValidator checks the number of selection and their values
22 * for a <b>TListControl that allows multiple selection</b>.
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.
31 * - At least two selections
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" />
39 * <com:TListControlValidator
40 * ControlToValidate="listbox"
42 * ErrorMessage="Please select at least 2" />
44 * - "value1" must be selected <b>and</b> at least 1 other
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>
52 * <com:TListControlValidator
53 * ControlToValidate="checkboxes"
54 * RequiredSelections="value1"
56 * ErrorMessage="Please select 'item1' and at least 1 other" />
59 * @author Xiang Wei Zhuo <weizhuo[at]gmail.com>
60 * @version $Id: TListControlValidator.php 3245 2013-01-07 20:23:32Z ctrlaltca $
61 * @package System.Web.UI.WebControls
64 class TListControlValidator extends TBaseValidator
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
71 protected function getClientClassName()
73 return 'Prado.WebUI.TListControlValidator';
77 * @return integer min number of selections. Defaults to -1, meaning not set.
79 public function getMinSelection()
81 return $this->getViewState('MinSelection',-1);
85 * @param integer minimum number of selections.
87 public function setMinSelection($value)
89 if(($value=TPropertyValue::ensureInteger($value))<0)
91 $this->setViewState('MinSelection',$value,-1);
95 * @return integer max number of selections. Defaults to -1, meaning not set.
97 public function getMaxSelection()
99 return $this->getViewState('MaxSelection',-1);
103 * @param integer max number of selections.
105 public function setMaxSelection($value)
107 if(($value=TPropertyValue::ensureInteger($value))<0)
109 $this->setViewState('MaxSelection',$value,-1);
113 * Get a comma separated list of required selected values.
114 * @return string comma separated list of required values.
116 public function getRequiredSelections()
118 return $this->getViewState('RequiredSelections','');
122 * Set the list of required values, using aa comma separated list.
123 * @param string comma separated list of required values.
125 public function setRequiredSelections($value)
127 $this->setViewState('RequiredSelections',$value,'');
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
136 protected function evaluateIsValid()
138 $control=$this->getValidationTarget();
141 $values = $this->getSelection($control);
142 $count = count($values);
143 $required = $this->getRequiredValues();
145 //if required, check the values
146 if(!empty($required))
148 if($count < count($required) )
150 foreach($required as $require)
151 $exists = $exists && in_array($require, $values);
154 $min = $this->getMinSelection();
155 $max = $this->getMaxSelection();
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;
168 * @param TListControl control to validate
169 * @return array number of selected values and its values.
171 protected function getSelection($control)
176 foreach($control->getItems() as $item)
178 if($item->getSelected())
179 $values[] = $item->getValue();
185 * @return array list of required values.
187 protected function getRequiredValues()
190 $string = $this->getRequiredSelections();
192 $required = preg_split('/,\s*/', $string);
197 * Returns an array of javascript validator options.
198 * @return array javascript validator options.
200 protected function getClientScriptOptions()
202 $options = parent::getClientScriptOptions();
203 $control = $this->getValidationTarget();
205 if(!$control instanceof TListControl)
207 throw new TConfigurationException(
208 'listcontrolvalidator_invalid_control',
209 $this->getID(),$this->getControlToValidate(), get_class($control));
212 $min = $this->getMinSelection();
213 $max = $this->getMaxSelection();
215 $options['Min']= $min;
217 $options['Max']= $max;
218 $required = $this->getRequiredSelections();
219 if(strlen($required) > 0)
220 $options['Required']= $required;
221 $options['TotalItems'] = $control->getItemCount();