Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TRequiredFieldValidator class file
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TRequiredFieldValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * Using TBaseValidator class
15
 */
16
Prado::using('System.Web.UI.WebControls.TBaseValidator');
17
 
18
/**
19
 * TRequiredFieldValidator class
20
 *
21
 * TRequiredFieldValidator makes the associated input control a required field.
22
 * The input control fails validation if its value does not change from
23
 * the {@link setInitialValue InitialValue} property upon losing focus.
24
 *
25
 * Validation will also succeed if input is of TListControl type and the number
26
 * of selected values different from the initial value is greater than zero.
27
 *
28
 * @author Qiang Xue <qiang.xue@gmail.com>
29
 * @version $Id: TRequiredFieldValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
30
 * @package System.Web.UI.WebControls
31
 * @since 3.0
32
 */
33
class TRequiredFieldValidator extends TBaseValidator
34
{
35
	/**
36
	 * Gets the name of the javascript class responsible for performing validation for this control.
37
	 * This method overrides the parent implementation.
38
	 * @return string the javascript class name
39
	 */
40
	protected function getClientClassName()
41
	{
42
		return 'Prado.WebUI.TRequiredFieldValidator';
43
	}
44
 
45
	/**
46
	 * @return string the initial value of the associated input control. Defaults to empty string.
47
	 * If the associated input control does not change from this initial value
48
	 * upon postback, the validation fails.
49
	 */
50
	public function getInitialValue()
51
	{
52
		return $this->getViewState('InitialValue','');
53
	}
54
 
55
	/**
56
	 * @param string the initial value of the associated input control.
57
	 * If the associated input control does not change from this initial value
58
	 * upon postback, the validation fails.
59
	 */
60
	public function setInitialValue($value)
61
	{
62
		$this->setViewState('InitialValue',TPropertyValue::ensureString($value),'');
63
	}
64
 
65
	/**
66
	 * This method overrides the parent's implementation.
67
	 * The validation succeeds if the input component changes its data
68
	 * from the {@link getInitialValue InitialValue} or the input control is not given.
69
	 *
70
	 * Validation will also succeed if input is of TListControl type and the
71
	 * number of selected values different from the initial value is greater
72
	 * than zero.
73
	 *
74
	 * @return boolean whether the validation succeeds
75
	 */
76
	protected function evaluateIsValid()
77
	{
78
		$control = $this->getValidationTarget();
79
		if($control instanceof TListControl)
80
			return $this->validateListControl($control);
81
		else if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0)
82
			return $this->validateRadioButtonGroup($control);
83
		else
84
			return $this->validateStandardControl($control);
85
	}
86
 
87
	private function validateListControl($control)
88
	{
89
		$initial = trim($this->getInitialValue());
90
		$count = 0;
91
		foreach($control->getItems() as $item)
92
		{
93
			if($item->getSelected() && $item->getValue() != $initial)
94
				$count++;
95
		}
96
		return $count > 0;
97
	}
98
 
99
	private function validateRadioButtonGroup($control)
100
	{
101
		$initial = trim($this->getInitialValue());
102
		foreach($control->getRadioButtonsInGroup() as $radio)
103
		{
104
			if($radio->getChecked())
105
			{
106
				if(strlen($value = $radio->getValue()) > 0)
107
					return $value !== $initial;
108
				else
109
					return true;
110
			}
111
		}
112
		return false;
113
	}
114
 
115
	private function validateStandardControl($control)
116
	{
117
		$initial = trim($this->getInitialValue());
118
		$value=$this->getValidationValue($control);
119
		return (is_bool($value) && $value) || trim($value)!==$initial;
120
	}
121
 
122
	/**
123
	 * Returns an array of javascript validator options.
124
	 * @return array javascript validator options.
125
	 */
126
	protected function getClientScriptOptions()
127
	{
128
		$options = parent::getClientScriptOptions();
129
		$options['InitialValue']=$this->getInitialValue();
130
		$control = $this->getValidationTarget();
131
		if($control instanceof TListControl)
132
			$options['TotalItems'] = $control->getItemCount();
133
		if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0)
134
			$options['GroupName'] = $control->getGroupName();
135
		return $options;
136
	}
137
}
138