]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TRequiredFieldValidator.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TRequiredFieldValidator.php
1 <?php
2 /**
3  * TRequiredFieldValidator class file
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.WebControls
10  */
11
12 /**
13  * Using TBaseValidator class
14  */
15 Prado::using('System.Web.UI.WebControls.TBaseValidator');
16
17 /**
18  * TRequiredFieldValidator class
19  *
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.
23  *
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.
26  *
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}.
29  *
30  * @author Qiang Xue <qiang.xue@gmail.com>
31  * @package System.Web.UI.WebControls
32  * @since 3.0
33  */
34 class TRequiredFieldValidator extends TBaseValidator
35 {
36         /**
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
40          */
41         protected function getClientClassName()
42         {
43                 return 'Prado.WebUI.TRequiredFieldValidator';
44         }
45
46         /**
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.
51          */
52         public function getInitialValue()
53         {
54                 return $this->getViewState('InitialValue',$this->getControlPromptValue());
55         }
56
57         /**
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.
61          */
62         protected function getControlPromptValue()
63         {
64                 $control = $this->getValidationTarget();
65                 if($control instanceof TListControl)
66                         return $control->getPromptValue();
67                 return '';
68         }
69         /**
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.
73          */
74         public function setInitialValue($value)
75         {
76                 $this->setViewState('InitialValue',TPropertyValue::ensureString($value),'');
77         }
78
79         /**
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.
83          *
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
86          * than zero.
87          *
88          * @return boolean whether the validation succeeds
89          */
90         protected function evaluateIsValid()
91         {
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);
97                 else
98                         return $this->validateStandardControl($control);
99         }
100
101         private function validateListControl($control)
102         {
103                 $initial = trim($this->getInitialValue());
104                 $count = 0;
105                 foreach($control->getItems() as $item)
106                 {
107                         if($item->getSelected() && $item->getValue() != $initial)
108                                 $count++;
109                 }
110                 return $count > 0;
111         }
112
113         private function validateRadioButtonGroup($control)
114         {
115                 $initial = trim($this->getInitialValue());
116                 foreach($control->getRadioButtonsInGroup() as $radio)
117                 {
118                         if($radio->getChecked())
119                         {
120                                 if(strlen($value = $radio->getValue()) > 0)
121                                         return $value !== $initial;
122                                 else
123                                         return true;
124                         }
125                 }
126                 return false;
127         }
128
129         private function validateStandardControl($control)
130         {
131                 $initial = trim($this->getInitialValue());
132                 $value=$this->getValidationValue($control);
133                 return (is_bool($value) && $value) || trim($value)!==$initial;
134         }
135
136         /**
137          * Returns an array of javascript validator options.
138          * @return array javascript validator options.
139          */
140         protected function getClientScriptOptions()
141         {
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();
149                 return $options;
150         }
151 }
152