3 * TRequiredFieldValidator class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.WebControls
13 * Using TBaseValidator class
15 Prado::using('System.Web.UI.WebControls.TBaseValidator');
18 * TRequiredFieldValidator class
20 * TRequiredFieldValidator makes the associated input control a required field.
21 * The input control fails validation if its value does not change from
22 * the {@link setInitialValue InitialValue} property upon losing focus.
24 * Validation will also succeed if input is of TListControl type and the number
25 * of selected values different from the initial value is greater than zero.
27 * If the input is of TListControl type and has a {@link TListControl::setPromptValue PromptValue}
28 * set, it will be automatically considered as the validator's {@link setInitialValue InitialValue}.
30 * @author Qiang Xue <qiang.xue@gmail.com>
31 * @package System.Web.UI.WebControls
34 class TRequiredFieldValidator extends TBaseValidator
37 * Gets the name of the javascript class responsible for performing validation for this control.
38 * This method overrides the parent implementation.
39 * @return string the javascript class name
41 protected function getClientClassName()
43 return 'Prado.WebUI.TRequiredFieldValidator';
47 * @return string the initial value of the associated input control. Defaults to empty string
48 * unless the control has a prompt value set.
49 * If the associated input control does not change from this initial value
50 * upon postback, the validation fails.
52 public function getInitialValue()
54 return $this->getViewState('InitialValue',$this->getControlPromptValue());
58 * @return string the initial value of the associated input control. Defaults to empty string.
59 * If the associated input control does not change from this initial value
60 * upon postback, the validation fails.
62 protected function getControlPromptValue()
64 $control = $this->getValidationTarget();
65 if($control instanceof TListControl)
66 return $control->getPromptValue();
70 * @param string the initial value of the associated input control.
71 * If the associated input control does not change from this initial value
72 * upon postback, the validation fails.
74 public function setInitialValue($value)
76 $this->setViewState('InitialValue',TPropertyValue::ensureString($value),'');
80 * This method overrides the parent's implementation.
81 * The validation succeeds if the input component changes its data
82 * from the {@link getInitialValue InitialValue} or the input control is not given.
84 * Validation will also succeed if input is of TListControl type and the
85 * number of selected values different from the initial value is greater
88 * @return boolean whether the validation succeeds
90 protected function evaluateIsValid()
92 $control = $this->getValidationTarget();
93 if($control instanceof TListControl)
94 return $this->validateListControl($control);
95 else if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0)
96 return $this->validateRadioButtonGroup($control);
98 return $this->validateStandardControl($control);
101 private function validateListControl($control)
103 $initial = trim($this->getInitialValue());
105 foreach($control->getItems() as $item)
107 if($item->getSelected() && $item->getValue() != $initial)
113 private function validateRadioButtonGroup($control)
115 $initial = trim($this->getInitialValue());
116 foreach($control->getRadioButtonsInGroup() as $radio)
118 if($radio->getChecked())
120 if(strlen($value = $radio->getValue()) > 0)
121 return $value !== $initial;
129 private function validateStandardControl($control)
131 $initial = trim($this->getInitialValue());
132 $value=$this->getValidationValue($control);
133 return (is_bool($value) && $value) || trim($value)!==$initial;
137 * Returns an array of javascript validator options.
138 * @return array javascript validator options.
140 protected function getClientScriptOptions()
142 $options = parent::getClientScriptOptions();
143 $options['InitialValue']=$this->getInitialValue();
144 $control = $this->getValidationTarget();
145 if($control instanceof TListControl)
146 $options['TotalItems'] = $control->getItemCount();
147 if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0)
148 $options['GroupName'] = $control->getGroupName();