/framework/Web/UI/WebControls/TListControlValidator.php

http://prado3.googlecode.com/ · PHP · 225 lines · 102 code · 20 blank · 103 comment · 23 complexity · bcc1dc3eb6e049d02e8085605f915820 MD5 · raw file

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