| [ Index ] |
|
Code source de PRADO 3.0.6 |
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 © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TValidationSummary.php 1397 2006-09-07 07:55:53Z wei $ 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 1397 2006-09-07 07:55:53Z wei $ 34 * @package System.Web.UI.WebControls 35 * @since 3.0 36 */ 37 class TValidationSummary extends TWebControl 38 { 39 /** 40 * @var TValidatorClientScript validator client-script options. 41 */ 42 private $_clientScript; 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->_clientScript)) 265 $options = array_merge($options, 266 $this->_clientScript->getOptions()->toArray()); 267 268 return $options; 269 } 270 271 /** 272 * @return TValidationSummaryClientScript client-side validation summary 273 * event options. 274 */ 275 public function getClientSide() 276 { 277 if(is_null($this->_clientScript)) 278 $this->_clientScript = $this->createClientScript(); 279 return $this->_clientScript; 280 } 281 282 /** 283 * @return TValidationSummaryClientScript javascript validation summary 284 * event options. 285 */ 286 protected function createClientScript() 287 { 288 return new TValidationSummaryClientScript; 289 } 290 /** 291 * Get the list of validation error messages. 292 * @return array list of validator error messages. 293 */ 294 protected function getErrorMessages() 295 { 296 $validators=$this->getPage()->getValidators($this->getValidationGroup()); 297 $messages = array(); 298 foreach($validators as $validator) 299 { 300 if(!$validator->getIsValid() && ($msg=$validator->getErrorMessage())!=='') 301 //$messages[] = $validator->getAnchoredMessage($msg); 302 $messages[] = $msg; 303 } 304 return $messages; 305 } 306 307 /** 308 * Overrides parent implementation by rendering TValidationSummary-specific presentation. 309 * @return string the rendering result 310 */ 311 public function renderContents($writer) 312 { 313 $this->renderJsSummary(); 314 if($this->getShowSummary()) 315 { 316 // $this->setStyle('display:block'); 317 switch($this->getDisplayMode()) 318 { 319 case TValidationSummaryDisplayMode::SimpleList: 320 $this->renderList($writer); 321 break; 322 case TValidationSummaryDisplayMode::SingleParagraph: 323 $this->renderSingleParagraph($writer); 324 break; 325 case TValidationSummaryDisplayMode::BulletList: 326 $this->renderBulletList($writer); 327 break; 328 } 329 } 330 } 331 332 /** 333 * Render the validation summary as a simple list. 334 * @param array list of messages 335 * @param string the header text 336 * @return string summary list 337 */ 338 protected function renderList($writer) 339 { 340 $header=$this->getHeaderText(); 341 $messages=$this->getErrorMessages(); 342 $content = ''; 343 if(strlen($header)) 344 $content.= $header."<br/>\n"; 345 foreach($messages as $message) 346 $content.="$message<br/>\n"; 347 $writer->write($content); 348 } 349 350 /** 351 * Render the validation summary as a paragraph. 352 * @param array list of messages 353 * @param string the header text 354 * @return string summary paragraph 355 */ 356 protected function renderSingleParagraph($writer) 357 { 358 $header=$this->getHeaderText(); 359 $messages=$this->getErrorMessages(); 360 $content = $header; 361 foreach($messages as $message) 362 $content.= ' '.$message; 363 $writer->write($content); 364 } 365 366 /** 367 * Render the validation summary as a bullet list. 368 * @param array list of messages 369 * @param string the header text 370 * @return string summary bullet list 371 */ 372 protected function renderBulletList($writer) 373 { 374 $header=$this->getHeaderText(); 375 $messages=$this->getErrorMessages(); 376 $content = $header; 377 if(count($messages)>0) 378 { 379 $content .= "<ul>\n"; 380 foreach($messages as $message) 381 $content.= '<li>'.$message."</li>\n"; 382 $content .= "</ul>\n"; 383 } 384 $writer->write($content); 385 } 386 } 387 388 /** 389 * TValidationSummaryClientScript class. 390 * 391 * Client-side validation summary events such as {@link setOnHideSummary 392 * OnHideSummary} and {@link setOnShowSummary OnShowSummary} can be modified 393 * through the {@link TBaseValidator:: getClientSide ClientSide} property of a 394 * validation summary. 395 * 396 * The <tt>OnHideSummary</tt> event is raise when the validation summary 397 * requests to hide the messages. 398 * 399 * The <tt>OnShowSummary</tt> event is raised when the validation summary 400 * requests to show the messages. 401 * 402 * See the quickstart documentation for further details. 403 * 404 * @author Wei Zhuo <weizhuo[at]gmail[dot]com> 405 * @version $Id: TValidationSummary.php 1397 2006-09-07 07:55:53Z wei $ 406 * @package System.Web.UI.WebControls 407 * @since 3.0 408 */ 409 class TValidationSummaryClientScript extends TClientSideOptions 410 { 411 /** 412 * @return string javascript code for client-side OnHideSummary event. 413 */ 414 public function getOnHideSummary() 415 { 416 return $this->getOption('OnHideSummary'); 417 } 418 419 /** 420 * Client-side OnHideSummary validation summary event is raise when all the 421 * validators are valid. This will override the default client-side 422 * validation summary behaviour. 423 * @param string javascript code for client-side OnHideSummary event. 424 */ 425 public function setOnHideSummary($javascript) 426 { 427 $this->setFunction('OnHideSummary', $javascript); 428 } 429 430 /** 431 * Client-side OnShowSummary event is raise when one or more validators are 432 * not valid. This will override the default client-side validation summary 433 * behaviour. 434 * @param string javascript code for client-side OnShowSummary event. 435 */ 436 public function setOnShowSummary($javascript) 437 { 438 $this->setFunction('OnShowSummary', $javascript); 439 } 440 441 /** 442 * @return string javascript code for client-side OnShowSummary event. 443 */ 444 public function getOnShowSummary() 445 { 446 return $this->getOption('OnShowSummary'); 447 } 448 } 449 450 451 /** 452 * TValidationSummaryDisplayMode class. 453 * TValidationSummaryDisplayMode defines the enumerable type for the possible modes 454 * that a {@link TValidationSummary} can organize and display the collected error messages. 455 * 456 * The following enumerable values are defined: 457 * - SimpleList: the error messages are displayed as a list without any decorations. 458 * - SingleParagraph: the error messages are concatenated together into a paragraph. 459 * - BulletList: the error messages are displayed as a bulleted list. 460 * 461 * @author Qiang Xue <qiang.xue@gmail.com> 462 * @version $Id: TValidationSummary.php 1397 2006-09-07 07:55:53Z wei $ 463 * @package System.Web.UI.WebControls 464 * @since 3.0.4 465 */ 466 class TValidationSummaryDisplayMode extends TEnumerable 467 { 468 const SimpleList='SimpleList'; 469 const SingleParagraph='SingleParagraph'; 470 const BulletList='BulletList'; 471 } 472 473 474 /** 475 * TValidationSummaryDisplay class. 476 * TValidationSummaryDisplay defines the enumerable type for the possible styles 477 * that a {@link TValidationSummary} can display the collected error messages. 478 * 479 * The following enumerable values are defined: 480 * - None: the error messages are not displayed 481 * - Dynamic: the error messages are dynamically added to display as the corresponding validators fail 482 * - Fixed: Similar to Dynamic except that the error messages physically occupy the page layout (even though they may not be visible) 483 * 484 * @author Qiang Xue <qiang.xue@gmail.com> 485 * @version $Id: TValidationSummary.php 1397 2006-09-07 07:55:53Z wei $ 486 * @package System.Web.UI.WebControls 487 * @since 3.0.4 488 */ 489 class TValidationSummaryDisplayStyle extends TEnumerable 490 { 491 const None='None'; 492 const Dynamic='Dynamic'; 493 const Fixed='Fixed'; 494 } 495 496 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |