Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TValidationSummary 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: TValidationSummary.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * TValidationSummary class
15
 *
16
 * TValidationSummary displays a summary of validation errors inline on a Web page,
17
 * in a message box, or both. By default, a validation summary will collect
18
 * {@link TBaseValidator::getErrorMessage ErrorMessage} of all failed validators
19
 * on the page. If {@link getValidationGroup ValidationGroup} is not
20
 * empty, only those validators who belong to the group will show their error messages
21
 * in the summary.
22
 *
23
 * The summary can be displayed as a list, as a bulleted list, or as a single
24
 * paragraph based on the {@link setDisplayMode DisplayMode} property.
25
 * The messages shown can be prefixed with {@link setHeaderText HeaderText}.
26
 *
27
 * The summary can be displayed on the Web page and in a message box by setting
28
 * the {@link setShowSummary ShowSummary} and {@link setShowMessageBox ShowMessageBox}
29
 * properties, respectively. Note, the latter is only effective when
30
 * {@link setEnableClientScript EnableClientScript} is true.
31
 *
32
 * @author Qiang Xue <qiang.xue@gmail.com>
33
 * @version $Id: TValidationSummary.php 2541 2008-10-21 15:05:13Z qiang.xue $
34
 * @package System.Web.UI.WebControls
35
 * @since 3.0
36
 */
37
class TValidationSummary extends TWebControl
38
{
39
	/**
40
	 * @var TClientSideValidationSummaryOptions validation client side options.
41
	 */
42
	private $_clientSide;
43
 
44
	/**
45
	 * Constructor.
46
	 * This method sets the foreground color to red.
47
	 */
48
	public function __construct()
49
	{
50
		parent::__construct();
51
		$this->setForeColor('red');
52
	}
53
 
54
	/**
55
	 * @return TValidationSummaryDisplayStyle the style of displaying the error messages. Defaults to TValidationSummaryDisplayStyle::Fixed.
56
	 */
57
	public function getDisplay()
58
	{
59
		return $this->getViewState('Display',TValidationSummaryDisplayStyle::Fixed);
60
	}
61
 
62
	/**
63
	 * @param TValidationSummaryDisplayStyle the style of displaying the error messages
64
	 */
65
	public function setDisplay($value)
66
	{
67
		$this->setViewState('Display',TPropertyValue::ensureEnum($value,'TValidationSummaryDisplayStyle'),TValidationSummaryDisplayStyle::Fixed);
68
	}
69
 
70
	/**
71
	 * @return string the header text displayed at the top of the summary
72
	 */
73
	public function getHeaderText()
74
	{
75
		return $this->getViewState('HeaderText','');
76
	}
77
 
78
	/**
79
	 * Sets the header text to be displayed at the top of the summary
80
	 * @param string the header text
81
	 */
82
	public function setHeaderText($value)
83
	{
84
		$this->setViewState('HeaderText',$value,'');
85
	}
86
 
87
	/**
88
	 * @return TValidationSummaryDisplayMode the mode of displaying error messages. Defaults to TValidationSummaryDisplayMode::BulletList.
89
	 */
90
	public function getDisplayMode()
91
	{
92
		return $this->getViewState('DisplayMode',TValidationSummaryDisplayMode::BulletList);
93
	}
94
 
95
	/**
96
	 * @param TValidationSummaryDisplayMode the mode of displaying error messages
97
	 */
98
	public function setDisplayMode($value)
99
	{
100
		$this->setViewState('DisplayMode',TPropertyValue::ensureEnum($value,'TValidationSummaryDisplayMode'),TValidationSummaryDisplayMode::BulletList);
101
	}
102
 
103
	/**
104
	 * @return boolean whether the TValidationSummary component updates itself using client-side script. Defaults to true.
105
	 */
106
	public function getEnableClientScript()
107
	{
108
		return $this->getViewState('EnableClientScript',true);
109
	}
110
 
111
	/**
112
	 * @param boolean whether the TValidationSummary component updates itself using client-side script.
113
	 */
114
	public function setEnableClientScript($value)
115
	{
116
		$this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
117
	}
118
 
119
	/**
120
	 * @return boolean whether the validation summary is displayed in a message box. Defaults to false.
121
	 */
122
	public function getShowMessageBox()
123
	{
124
		return $this->getViewState('ShowMessageBox',false);
125
	}
126
 
127
	/**
128
	 * @param boolean whether the validation summary is displayed in a message box.
129
	 */
130
	public function setShowMessageBox($value)
131
	{
132
		$this->setViewState('ShowMessageBox',TPropertyValue::ensureBoolean($value),false);
133
	}
134
 
135
	/**
136
	 * @return boolean whether the validation summary is displayed inline. Defaults to true.
137
	 */
138
	public function getShowSummary()
139
	{
140
		return $this->getViewState('ShowSummary',true);
141
	}
142
 
143
	/**
144
	 * @param boolean whether the validation summary is displayed inline.
145
	 */
146
	public function setShowSummary($value)
147
	{
148
		$this->setViewState('ShowSummary',TPropertyValue::ensureBoolean($value),true);
149
	}
150
 
151
	/**
152
	 * @return boolean whether the validation summary should be anchored. Defaults to false.
153
	 */
154
	public function getShowAnchor()
155
	{
156
		return $this->getViewState('ShowAnchor',false);
157
	}
158
 
159
	/**
160
	 * @param boolean whether the validation summary should be anchored.
161
	 */
162
	public function setShowAnchor($value)
163
	{
164
		$this->setViewState('ShowAnchor',TPropertyValue::ensureBoolean($value),false);
165
	}
166
 
167
	/**
168
	 * Gets the auto-update for this summary.
169
	 * @return boolean automatic client-side summary updates. Defaults to true.
170
	 */
171
	public function getAutoUpdate()
172
	{
173
		return $this->getViewState('AutoUpdate', true);
174
	}
175
 
176
	/**
177
	 * Sets the summary to auto-update on the client-side
178
	 * @param boolean true for automatic summary updates.
179
	 */
180
	public function setAutoUpdate($value)
181
	{
182
		$this->setViewState('AutoUpdate', TPropertyValue::ensureBoolean($value), true);
183
	}
184
 
185
	/**
186
	 * @return string the group which this validator belongs to
187
	 */
188
	public function getValidationGroup()
189
	{
190
		return $this->getViewState('ValidationGroup','');
191
	}
192
 
193
	/**
194
	 * @param string the group which this validator belongs to
195
	 */
196
	public function setValidationGroup($value)
197
	{
198
		$this->setViewState('ValidationGroup',$value,'');
199
	}
200
 
201
	protected function addAttributesToRender($writer)
202
	{
203
		$display=$this->getDisplay();
204
		$visible=$this->getEnabled(true) && count($this->getErrorMessages()) > 0;
205
		if(!$visible)
206
		{
207
			if($display===TValidationSummaryDisplayStyle::None || $display===TValidationSummaryDisplayStyle::Dynamic)
208
				$writer->addStyleAttribute('display','none');
209
			else
210
				$writer->addStyleAttribute('visibility','hidden');
211
		}
212
		$writer->addAttribute('id',$this->getClientID());
213
		parent::addAttributesToRender($writer);
214
	}
215
 
216
	/**
217
	 * Render the javascript for validation summary.
218
	 * @param array list of options for validation summary.
219
	 */
220
	protected function renderJsSummary()
221
	{
222
		if(!$this->getEnabled(true) || !$this->getEnableClientScript())
223
			return;
224
		$cs = $this->getPage()->getClientScript();
225
		$cs->registerPradoScript('validator');
226
 
227
		//need to register the validation manager is validation summary is alone.
228
		$formID=$this->getPage()->getForm()->getClientID();
229
		$scriptKey = "TBaseValidator:$formID";
230
		if($this->getEnableClientScript() && !$cs->isEndScriptRegistered($scriptKey))
231
		{
232
			$manager['FormID'] = $formID;
233
			$options = TJavaScript::encode($manager);
234
			$cs->registerPradoScript('validator');
235
			$cs->registerEndScript($scriptKey, "new Prado.ValidationManager({$options});");
236
		}
237
 
238
 
239
		$options=TJavaScript::encode($this->getClientScriptOptions());
240
		$script = "new Prado.WebUI.TValidationSummary({$options});";
241
		$cs->registerEndScript($this->getClientID(), $script);
242
	}
243
 
244
	/**
245
	 * Get a list of options for the client-side javascript validation summary.
246
	 * @return array list of options for the summary
247
	 */
248
	protected function getClientScriptOptions()
249
	{
250
		$options['ID'] = $this->getClientID();
251
		$options['FormID'] = $this->getPage()->getForm()->getClientID();
252
		if($this->getShowMessageBox())
253
			$options['ShowMessageBox']=true;
254
		if(!$this->getShowSummary())
255
			$options['ShowSummary']=false;
256
 
257
		$options['HeaderText']=$this->getHeaderText();
258
		$options['DisplayMode']=$this->getDisplayMode();
259
 
260
		$options['Refresh'] = $this->getAutoUpdate();
261
		$options['ValidationGroup'] =  $this->getValidationGroup();
262
		$options['Display'] = $this->getDisplay();
263
 
264
		if(!is_null($this->_clientSide))
265
			$options = array_merge($options,$this->_clientSide->getOptions()->toArray());
266
 
267
		return $options;
268
	}
269
 
270
	/**
271
	 * @return TClientSideValidationSummaryOptions client-side validation summary
272
	 * event options.
273
	 */
274
	public function getClientSide()
275
	{
276
		if(is_null($this->_clientSide))
277
			$this->_clientSide = $this->createClientScript();
278
		return $this->_clientSide;
279
	}
280
 
281
	/**
282
	 * @return TClientSideValidationSummaryOptions javascript validation summary
283
	 * event options.
284
	 */
285
	protected function createClientScript()
286
	{
287
		return new TClientSideValidationSummaryOptions;
288
	}
289
	/**
290
	 * Get the list of validation error messages.
291
	 * @return array list of validator error messages.
292
	 */
293
	protected function getErrorMessages()
294
	{
295
		$validators=$this->getPage()->getValidators($this->getValidationGroup());
296
		$messages = array();
297
		foreach($validators as $validator)
298
		{
299
			if(!$validator->getIsValid() && ($msg=$validator->getErrorMessage())!=='')
300
				//$messages[] = $validator->getAnchoredMessage($msg);
301
				$messages[] = $msg;
302
		}
303
		return $messages;
304
	}
305
 
306
	/**
307
	 * Overrides parent implementation by rendering TValidationSummary-specific presentation.
308
	 * @return string the rendering result
309
	 */
310
	public function renderContents($writer)
311
	{
312
		$this->renderJsSummary();
313
		if($this->getShowSummary())
314
		{
315
//		    $this->setStyle('display:block');
316
			switch($this->getDisplayMode())
317
			{
318
				case TValidationSummaryDisplayMode::SimpleList:
319
					$this->renderList($writer);
320
					break;
321
				case TValidationSummaryDisplayMode::SingleParagraph:
322
					$this->renderSingleParagraph($writer);
323
					break;
324
				case TValidationSummaryDisplayMode::BulletList:
325
					$this->renderBulletList($writer);
326
					break;
327
			}
328
		}
329
	}
330
 
331
	/**
332
	 * Render the validation summary as a simple list.
333
	 * @param array list of messages
334
	 * @param string the header text
335
	 * @return string summary list
336
	 */
337
	protected function renderList($writer)
338
	{
339
		$header=$this->getHeaderText();
340
		$messages=$this->getErrorMessages();
341
		$content = '';
342
		if(strlen($header))
343
			$content.= $header."<br/>\n";
344
		foreach($messages as $message)
345
			$content.="$message<br/>\n";
346
		$writer->write($content);
347
	}
348
 
349
	/**
350
	 * Render the validation summary as a paragraph.
351
	 * @param array list of messages
352
	 * @param string the header text
353
	 * @return string summary paragraph
354
	 */
355
	protected function renderSingleParagraph($writer)
356
	{
357
		$header=$this->getHeaderText();
358
		$messages=$this->getErrorMessages();
359
		$content = $header;
360
		foreach($messages as $message)
361
			$content.= ' '.$message;
362
		$writer->write($content);
363
	}
364
 
365
	/**
366
	 * Render the validation summary as a bullet list.
367
	 * @param array list of messages
368
	 * @param string the header text
369
	 * @return string summary bullet list
370
	 */
371
	protected function renderBulletList($writer)
372
	{
373
		$header=$this->getHeaderText();
374
		$messages=$this->getErrorMessages();
375
		$content = $header;
376
		if(count($messages)>0)
377
		{
378
			$content .= "<ul>\n";
379
			foreach($messages as $message)
380
				$content.= '<li>'.$message."</li>\n";
381
			$content .= "</ul>\n";
382
		}
383
		$writer->write($content);
384
	}
385
}
386
 
387
/**
388
 * TClientSideValidationSummaryOptions class.
389
 *
390
 * Client-side validation summary events such as {@link setOnHideSummary
391
 * OnHideSummary} and {@link setOnShowSummary OnShowSummary} can be modified
392
 * through the {@link TBaseValidator:: getClientSide ClientSide} property of a
393
 * validation summary.
394
 *
395
 * The <tt>OnHideSummary</tt> event is raise when the validation summary
396
 * requests to hide the messages.
397
 *
398
 * The <tt>OnShowSummary</tt> event is raised when the validation summary
399
 * requests to show the messages.
400
 *
401
 * See the quickstart documentation for further details.
402
 *
403
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
404
 * @version $Id: TValidationSummary.php 2541 2008-10-21 15:05:13Z qiang.xue $
405
 * @package System.Web.UI.WebControls
406
 * @since 3.0
407
 */
408
class TClientSideValidationSummaryOptions extends TClientSideOptions
409
{
410
	/**
411
	 * @return string javascript code for client-side OnHideSummary event.
412
	 */
413
	public function getOnHideSummary()
414
	{
415
		return $this->getOption('OnHideSummary');
416
	}
417
 
418
	/**
419
	 * Client-side OnHideSummary validation summary event is raise when all the
420
	 * validators are valid. This will override the default client-side
421
	 * validation summary behaviour.
422
	 * @param string javascript code for client-side OnHideSummary event.
423
	 */
424
	public function setOnHideSummary($javascript)
425
	{
426
		$this->setFunction('OnHideSummary', $javascript);
427
	}
428
 
429
	/**
430
	 * Client-side OnShowSummary event is raise when one or more validators are
431
	 * not valid. This will override the default client-side validation summary
432
	 * behaviour.
433
	 * @param string javascript code for client-side OnShowSummary event.
434
	 */
435
	public function setOnShowSummary($javascript)
436
	{
437
		$this->setFunction('OnShowSummary', $javascript);
438
	}
439
 
440
	/**
441
	 * @return string javascript code for client-side OnShowSummary event.
442
	 */
443
	public function getOnShowSummary()
444
	{
445
		return $this->getOption('OnShowSummary');
446
	}
447
 
448
	/**
449
	 * Ensure the string is a valid javascript function. If the string begins
450
	 * with "javascript:" valid javascript function is assumed, otherwise the
451
	 * code block is enclosed with "function(summary, validators){ }" block.
452
	 * @param string javascript code.
453
	 * @return string javascript function code.
454
	 */
455
	protected function ensureFunction($javascript)
456
	{
457
		return "function(summary, validators){ {$javascript} }";
458
	}
459
}
460
 
461
 
462
/**
463
 * TValidationSummaryDisplayMode class.
464
 * TValidationSummaryDisplayMode defines the enumerable type for the possible modes
465
 * that a {@link TValidationSummary} can organize and display the collected error messages.
466
 *
467
 * The following enumerable values are defined:
468
 * - SimpleList: the error messages are displayed as a list without any decorations.
469
 * - SingleParagraph: the error messages are concatenated together into a paragraph.
470
 * - BulletList: the error messages are displayed as a bulleted list.
471
 *
472
 * @author Qiang Xue <qiang.xue@gmail.com>
473
 * @version $Id: TValidationSummary.php 2541 2008-10-21 15:05:13Z qiang.xue $
474
 * @package System.Web.UI.WebControls
475
 * @since 3.0.4
476
 */
477
class TValidationSummaryDisplayMode extends TEnumerable
478
{
479
	const SimpleList='SimpleList';
480
	const SingleParagraph='SingleParagraph';
481
	const BulletList='BulletList';
482
}
483
 
484
 
485
/**
486
 * TValidationSummaryDisplay class.
487
 * TValidationSummaryDisplay defines the enumerable type for the possible styles
488
 * that a {@link TValidationSummary} can display the collected error messages.
489
 *
490
 * The following enumerable values are defined:
491
 * - None: the error messages are not displayed
492
 * - Dynamic: the error messages are dynamically added to display as the corresponding validators fail
493
 * - Fixed: Similar to Dynamic except that the error messages physically occupy the page layout (even though they may not be visible)
494
 *
495
 * @author Qiang Xue <qiang.xue@gmail.com>
496
 * @version $Id: TValidationSummary.php 2541 2008-10-21 15:05:13Z qiang.xue $
497
 * @package System.Web.UI.WebControls
498
 * @since 3.0.4
499
 */
500
class TValidationSummaryDisplayStyle extends TEnumerable
501
{
502
	const None='None';
503
	const Dynamic='Dynamic';
504
	const Fixed='Fixed';
505
}
506