summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/WebControls/TRequiredFieldValidator.php
blob: 7cb439b75580ae007daa30d046bd6ecff4c8026d (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
<?php
/**
 * TRequiredFieldValidator class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.WebControls
 */

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

/**
 * TRequiredFieldValidator class
 *
 * TRequiredFieldValidator makes the associated input control a required field.
 * The input control fails validation if its value does not change from
 * the {@link setInitialValue InitialValue} property upon losing focus.
 *
 * Validation will also succeed if input is of TListControl type and the number
 * of selected values different from the initial value is greater than zero.
 *
 * If the input is of TListControl type and has a {@link TListControl::setPromptValue PromptValue}
 * set, it will be automatically considered as the validator's {@link setInitialValue InitialValue}.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TRequiredFieldValidator 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.TRequiredFieldValidator';
	}

	/**
	 * @return string the initial value of the associated input control. Defaults to empty string
	 * unless the control has a prompt value set.
	 * If the associated input control does not change from this initial value
	 * upon postback, the validation fails.
	 */
	public function getInitialValue()
	{
		return $this->getViewState('InitialValue',$this->getControlPromptValue());
	}

	/**
	 * @return string the initial value of the associated input control. Defaults to empty string.
	 * If the associated input control does not change from this initial value
	 * upon postback, the validation fails.
	 */
	protected function getControlPromptValue()
	{
		$control = $this->getValidationTarget();
		if($control instanceof TListControl)
			return $control->getPromptValue();
		return '';
	}
	/**
	 * @param string the initial value of the associated input control.
	 * If the associated input control does not change from this initial value
	 * upon postback, the validation fails.
	 */
	public function setInitialValue($value)
	{
		$this->setViewState('InitialValue',TPropertyValue::ensureString($value),'');
	}

	/**
	 * This method overrides the parent's implementation.
	 * The validation succeeds if the input component changes its data
	 * from the {@link getInitialValue InitialValue} or the input control is not given.
	 *
	 * Validation will also succeed if input is of TListControl type and the
	 * number of selected values different from the initial value is greater
	 * than zero.
	 *
	 * @return boolean whether the validation succeeds
	 */
	protected function evaluateIsValid()
	{
		$control = $this->getValidationTarget();
		if($control instanceof TListControl)
			return $this->validateListControl($control);
		else if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0)
			return $this->validateRadioButtonGroup($control);
		else
			return $this->validateStandardControl($control);
	}

	private function validateListControl($control)
	{
		$initial = trim($this->getInitialValue());
		$count = 0;
		foreach($control->getItems() as $item)
		{
			if($item->getSelected() && $item->getValue() != $initial)
				$count++;
		}
		return $count > 0;
	}

	private function validateRadioButtonGroup($control)
	{
		$initial = trim($this->getInitialValue());
		foreach($control->getRadioButtonsInGroup() as $radio)
		{
			if($radio->getChecked())
			{
				if(strlen($value = $radio->getValue()) > 0)
					return $value !== $initial;
				else
					return true;
			}
		}
		return false;
	}

	private function validateStandardControl($control)
	{
		$initial = trim($this->getInitialValue());
		$value=$this->getValidationValue($control);
		return (is_bool($value) && $value) || trim($value)!==$initial;
	}

	/**
	 * Returns an array of javascript validator options.
	 * @return array javascript validator options.
	 */
	protected function getClientScriptOptions()
	{
		$options = parent::getClientScriptOptions();
		$options['InitialValue']=$this->getInitialValue();
		$control = $this->getValidationTarget();
		if($control instanceof TListControl)
			$options['TotalItems'] = $control->getItemCount();
		if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0)
			$options['GroupName'] = $control->getGroupName();
		return $options;
	}
}