summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TListControlValidator.php
blob: 04cc6ef61baabd88d45358f9ab8f79c893b4dd53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
 * TListControlValidator class file
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package System.Web.UI.WebControls
 */

/**
 * Using TBaseValidator class
 */
Prado::using('System.Web.UI.WebControls.TBaseValidator');

/**
 * TListControlValidator class.
 *
 * TListControlValidator checks the number of selection and their values
 * for a <b>TListControl that allows multiple selection</b>.
 *
 * You can specify the minimum or maximum (or both) number of selections
 * required using the {@link setMinSelection MinSelection} and
 * {@link setMaxSelection MaxSelection} properties, respectively. In addition,
 * you can specify a comma separated list of required selected values via the
 * {@link setRequiredSelections RequiredSelections} property.
 *
 * Examples
 * - At least two selections
 * <code>
 *	<com:TListBox ID="listbox" SelectionMode="Multiple">
 *		<com:TListItem Text="item1" Value="value1" />
 *		<com:TListItem Text="item2" Value="value2" />
 *		<com:TListItem Text="item3" Value="value3" />
 *	</com:TListBox>
 *
 *	<com:TListControlValidator
 *		ControlToValidate="listbox"
 *		MinSelection="2"
 *		ErrorMessage="Please select at least 2" />
 * </code>
 * - "value1" must be selected <b>and</b> at least 1 other
 * <code>
 *	<com:TCheckBoxList ID="checkboxes">
 *		<com:TListItem Text="item1" Value="value1" />
 *		<com:TListItem Text="item2" Value="value2" />
 *		<com:TListItem Text="item3" Value="value3" />
 *	</com:TCheckBoxList>
 *
 *	<com:TListControlValidator
 *		ControlToValidate="checkboxes"
 *		RequiredSelections="value1"
 *		MinSelection="2"
 *		ErrorMessage="Please select 'item1' and at least 1 other" />
 * </code>
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TListControlValidator extends TBaseValidator
{
	/**
	 * Gets the name of the javascript class responsible for performing validation for this control.
	 * This method overrides the parent implementation.
	 * @return string the javascript class name
	 */
	protected function getClientClassName()
	{
		return 'Prado.WebUI.TListControlValidator';
	}

	/**
	 * @return integer min number of selections. Defaults to -1, meaning not set.
	 */
	public function getMinSelection()
	{
		return $this->getViewState('MinSelection',-1);
	}

	/**
	 * @param integer minimum number of selections.
	 */
	public function setMinSelection($value)
	{
		if(($value=TPropertyValue::ensureInteger($value))<0)
			$value=-1;
		$this->setViewState('MinSelection',$value,-1);
	}

	/**
	 * @return integer max number of selections.  Defaults to -1, meaning not set.
	 */
	public function getMaxSelection()
	{
		return $this->getViewState('MaxSelection',-1);
	}

	/**
	 * @param integer max number of selections.
	 */
	public function setMaxSelection($value)
	{
		if(($value=TPropertyValue::ensureInteger($value))<0)
			$value=-1;
		$this->setViewState('MaxSelection',$value,-1);
	}

	/**
	 * Get a comma separated list of required selected values.
	 * @return string comma separated list of required values.
	 */
	public function getRequiredSelections()
	{
		return $this->getViewState('RequiredSelections','');
	}

	/**
	 * Set the list of required values, using aa comma separated list.
	 * @param string comma separated list of required values.
	 */
	public function setRequiredSelections($value)
	{
		$this->setViewState('RequiredSelections',$value,'');
	}

	/**
	 * This method overrides the parent's implementation.
	 * The validation succeeds if the input component changes its data
	 * from the InitialValue or the input component is not given.
	 * @return boolean whether the validation succeeds
	 */
	protected function evaluateIsValid()
	{
		$control=$this->getValidationTarget();

		$exists = true;
		$values = $this->getSelection($control);
		$count = count($values);
		$required = $this->getRequiredValues();

		//if required, check the values
		if(!empty($required))
		{
			if($count < count($required) )
				return false;
			foreach($required as $require)
				$exists = $exists && in_array($require, $values);
		}

		$min = $this->getMinSelection();
		$max = $this->getMaxSelection();

		if($min !== -1 && $max !== -1)
			return $exists && $count >= $min && $count <= $max;
		else if($min === -1 && $max !== -1)
			return $exists && $count <= $max;
		else if($min !== -1 && $max === -1)
			return $exists && $count >= $min;
		else
			return $exists;
	}

	/**
	 * @param TListControl control to validate
	 * @return array number of selected values and its values.
	 */
	protected function getSelection($control)
	{
		$values = array();

		//get the data
		foreach($control->getItems() as $item)
		{
			if($item->getSelected())
				$values[] = $item->getValue();
		}
		return $values;
	}

	/**
	 * @return array list of required values.
	 */
	protected function getRequiredValues()
	{
		$required = array();
		$string = $this->getRequiredSelections();
		if(!empty($string))
			$required = preg_split('/,\s*/', $string);
		return $required;
	}

	/**
	 * Returns an array of javascript validator options.
	 * @return array javascript validator options.
	 */
	protected function getClientScriptOptions()
	{
		$options = parent::getClientScriptOptions();
		$control = $this->getValidationTarget();

		if(!$control instanceof TListControl)
		{
			throw new TConfigurationException(
				'listcontrolvalidator_invalid_control',
				$this->getID(),$this->getControlToValidate(), get_class($control));
		}

		$min = $this->getMinSelection();
		$max = $this->getMaxSelection();
		if($min !== -1)
			$options['Min']= $min;
		if($max !== -1)
			$options['Max']= $max;
		$required = $this->getRequiredSelections();
		if(strlen($required) > 0)
			$options['Required']= $required;
		$options['TotalItems'] = $control->getItemCount();

		return $options;
	}
}