Changeset 1444
- Timestamp:
- 12/25/06 23:39:25 (2 years ago)
- Location:
- branches/0.11
- Files:
-
- 2 added
- 8 modified
-
src/request/AgaviRequest.class.php (modified) (11 diffs)
-
src/validator/AgaviBaseFileValidator.class.php (modified) (2 diffs)
-
src/validator/AgaviIValidatorContainer.interface.php (modified) (2 diffs)
-
src/validator/AgaviIssetValidator.class.php (modified) (2 diffs)
-
src/validator/AgaviOperatorValidator.class.php (modified) (3 diffs)
-
src/validator/AgaviValidationError.class.php (added)
-
src/validator/AgaviValidationIncident.class.php (added)
-
src/validator/AgaviValidator.class.php (modified) (11 diffs)
-
src/validator/AgaviValidatorManager.class.php (modified) (10 diffs)
-
tests2/request/RequestTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.11/src/request/AgaviRequest.class.php
r1239 r1444 96 96 public function getError($name) 97 97 { 98 $errors = $this->getAttribute('errors', 'org.agavi.validation.result', array()); 99 $retval = null; 100 101 if(isset($errors[$name]['messages'][0])) { 102 $retval = $errors[$name]['messages'][0]; 103 } 104 105 return $retval; 98 $vm = $this->getContext()->getValidatorManager(); 99 $incidents = $vm->getFieldIncidents($name, AgaviValidator::NOTICE); 100 101 if(count($incidents) == 0) { 102 return null; 103 } 104 105 $errors = $incidents[0]->getErrors(); 106 return $errors[0]->getMessage(); 106 107 } 107 108 … … 117 118 public function getErrorNames() 118 119 { 119 $errors = $this->getAttribute('errors', 'org.agavi.validation.result', array()); 120 if(isset($errors[''])) { 121 unset($errors['']); 122 } 123 return array_keys($errors); 120 return $this->getContext()->getValidatorManager()->getFailedFields(); 124 121 } 125 122 … … 139 136 public function getErrors($name = null) 140 137 { 141 $errors = $this->getAttribute('errors', 'org.agavi.validation.result', array()); 138 $vm = $this->getContext()->getValidatorManager(); 139 $errors = array(); 140 141 foreach($vm->getIncidents(AgaviValidator::NOTICE) as $incident) { 142 $validator = $incident->getValidator(); 143 foreach($incident->getErrors() as $error) { 144 $msg = $error->getMessage(); 145 foreach($error->getFields() as $field) { 146 if(!isset($errors[$field])) { 147 $errors[$field] = array('messages' => array(), 'validators' => array()); 148 } 149 $errors[$field]['messages'][] = $msg; 150 if($validator) { 151 $errors[$field]['validators'][] = $validator->getName(); 152 } 153 } 154 } 155 } 156 142 157 if($name === null) { 143 158 return $errors; … … 161 176 public function getErrorMessages($name = null) 162 177 { 163 $ errors = $this->getAttribute('errors', 'org.agavi.validation.result', array());178 $vm = $this->getContext()->getValidatorManager(); 164 179 165 180 if($name !== null) { 166 return isset($errors[$name]['messages']) ? $errors[$name]['messages'] : null; 181 $incidents = $vm->getFieldIncidents($name, AgaviValidator::NOTICE); 182 $msgs = array(); 183 foreach($incidents as $incident) { 184 foreach($incident->getErrors() as $error) { 185 $msgs[] = $error->getMessage(); 186 } 187 } 188 return $msgs; 167 189 } else { 168 190 $msgs = array(); 169 191 170 foreach($errors as $errorName => $error) { 171 foreach($error['messages'] as $message) { 172 if(!isset($msgs[$message])) { 173 $msgs[$message] = array(); 174 } 175 $msgs[$message][] = $errorName; 192 $incidents = $vm->getIncidents(AgaviValidator::NOTICE); 193 $msgs = array(); 194 foreach($incidents as $incident) { 195 foreach($incident->getErrors() as $error) { 196 $msgs[] = array('message' => $error->getMessage(), 'errors' => $error->getFields()); 176 197 } 177 198 } 178 179 $retMsgs = array(); 180 $i = 0; 181 foreach($msgs as $message => $errorNames) { 182 $retMsgs[$i] = array('message' => $message, 'errors' => $errorNames); 183 ++$i; 184 } 185 return $retMsgs; 199 return $msgs; 186 200 } 187 201 } … … 214 228 public function hasError($name) 215 229 { 216 $errors = $this->getAttribute('errors', 'org.agavi.validation.result', array()); 217 return isset($errors[$name]); 230 return $this->getContext()->getValidatorManager()->isFieldFailed($name); 218 231 } 219 232 … … 230 243 public function hasErrors() 231 244 { 232 return (count($this->getAttribute('errors', 'org.agavi.validation.result', array())) > 0);245 return $this->getContext()->getValidatorManager()->getResult() > AgaviValidator::NOTICE; 233 246 } 234 247 … … 262 275 263 276 /** 264 * Remove an error.265 *266 * @param string An error name.267 *268 * @return string An error message, if the error was removed, otherwise269 * null.270 *271 * @author Sean Kerr <skerr@mojavi.org>272 * @author David Zuelke <dz@bitxtender.com>273 * @author Dominik del Bondio <ddb@bitxtender.com>274 * @since 0.9.0275 */276 public function removeError($name)277 {278 $errors =& $this->getAttribute('errors', 'org.agavi.validation.result', array());279 $retval = null;280 281 if(isset($errors[$name])) {282 $retval = $errors[$name];283 unset($errors[$name]);284 }285 286 return $retval;287 }288 289 /**290 277 * Set an error. 291 278 * … … 293 280 * @param string An error message. 294 281 * 295 * @author Sean Kerr <skerr@mojavi.org>296 282 * @author Dominik del Bondio <ddb@bitxtender.com> 297 283 * @since 0.9.0 … … 299 285 public function setError($name, $message) 300 286 { 301 // set the attribute first if it doesn't exist, else we will not a proper 302 // reference to the attribute. 303 if(!$this->hasAttribute('errors', 'org.agavi.validation.result')) { 304 $this->setAttribute('errors', array(), 'org.agavi.validation.result'); 305 } 306 $errors =& $this->getAttribute('errors', 'org.agavi.validation.result'); 307 if(!isset($errors[$name])) { 308 $errors[$name] = array('messages' => array(), 'validators' => array()); 309 } 310 $errors[$name]['messages'][] = $message; 287 $vm = $this->getContext()->getValidatorManager(); 288 $incident = new AgaviValidationIncident(null, AgaviValidator::ERROR); 289 $incident->addError(new AgaviValidationError($message, null, array($name))); 290 $vm->addIncident($incident); 311 291 } 312 292 … … 321 301 * messages. 322 302 * 323 * @author Sean Kerr <skerr@mojavi.org>324 303 * @author Dominik del Bondio <ddb@bitxtender.com> 325 304 * @since 0.9.0 … … 327 306 public function setErrors(array $errors) 328 307 { 329 // set the attribute first if it doesn't exist, else we will not a proper 330 // reference to the attribute. 331 if(!$this->hasAttribute('errors', 'org.agavi.validation.result')) { 332 $this->setAttribute('errors', array(), 'org.agavi.validation.result'); 333 } 334 $storedErrors =& $this->getAttribute('errors', 'org.agavi.validation.result', array()); 308 $vm = $this->getContext()->getValidatorManager(); 309 $incident = new AgaviValidationIncident(null, AgaviValidator::ERROR); 335 310 foreach($errors as $name => $error) { 336 if(!isset($storedErrors[$name])) { 337 $storedErrors[$name] = array('messages' => array(), 'validators' => array()); 338 } 339 if(!is_array($error)) { 340 $storedErrors[$name]['messages'][] = $error; 341 } else { 342 $storedErrors[$name]['messages'] = array_merge($storedErrors[$name]['messages'], $error); 343 } 344 } 311 $incident->addError(new AgaviValidationError($error, null, array($name))); 312 } 313 314 $vm->addIncident($incident); 345 315 } 346 316 -
branches/0.11/src/validator/AgaviBaseFileValidator.class.php
r1430 r1444 41 41 abstract class AgaviBaseFileValidator extends AgaviValidator 42 42 { 43 43 44 /** 44 * Returns whether all arguments are files in the request. 45 * Returns whether all arguments are set in the validation input parameters. 46 * Set means anything but empty string. 47 * 48 * @param bool Whether an error should be thrown for each missing 49 * argument if this validator is required. 45 50 * 46 51 * @return bool Whether the arguments are set. … … 49 54 * @since 0.11.0 50 55 */ 51 protected function hasAllArgumentsSet()56 protected function checkAllArgumentsSet($throwError = true) 52 57 { 53 58 $request = $this->getContext()->getRequest(); 59 60 $isRequired = $this->getParameter('required', true); 61 $result = true; 62 63 $array = $this->validationParameters->getParameters(); 64 $baseParts = $this->curBase->getParts(); 54 65 foreach($this->getArguments() as $argument) { 55 66 $new = $this->curBase->pushRetNew($argument); 56 67 $pName = $this->curBase->pushRetNew($argument)->__toString(); 57 68 if(!$request->hasFile($pName)) { 58 return false; 69 if($throwError && $isRequired) { 70 $this->throwError(null, $pName); 71 } 72 $result = false; 59 73 } 60 74 } 61 return true;75 return $result; 62 76 } 63 64 77 65 78 /** -
branches/0.11/src/validator/AgaviIValidatorContainer.interface.php
r1245 r1444 40 40 41 41 /** 42 * Fetches the request.43 *44 * @return AgaviRequest The request to be used by child validators.45 *46 * @author Uwe Mesecke <uwe@mesecke.net>47 * @since 0.11.048 */49 public function getRequest();50 51 /**52 42 * Fetches the dependency manager 53 43 * … … 60 50 public function getDependencyManager(); 61 51 62 /**63 * Reports an error to the parent container.64 *65 * @param AgaviValidator The validator where the error occured.66 * @param string An error message.67 *68 * @author Dominik del Bondio <ddb@bitxtender.com>69 * @since 0.11.070 */71 public function reportError(AgaviValidator $validator, $errorMsg);72 52 } 73 53 ?> -
branches/0.11/src/validator/AgaviIssetValidator.class.php
r1245 r1444 32 32 { 33 33 /** 34 * We return true here no matter what because we will check for existance 35 * ourself. 36 * 37 * @param bool Whether an error should be thrown for each missing 38 * argument if this validator is required. 39 * 40 * @return bool Whether the arguments are set. 41 * 42 * @author Dominik del Bondio <ddb@bitxtender.com> 43 * @since 0.11.0 44 */ 45 protected function checkAllArgumentsSet($throwError = true) 46 { 47 return true; 48 } 49 50 /** 34 51 * Validates the input. 35 52 * … … 41 58 protected function validate() 42 59 { 60 $params = $this->validationParameters->getParameters(); 61 43 62 foreach($this->getArguments() as $argument) { 44 if(!$this-> parentContainer->getRequest()->hasParameter($argument)) {63 if(!$this->curBase->hasValueByChildPath($argument, $params)) { 45 64 $this->throwError(); 46 65 return false; -
branches/0.11/src/validator/AgaviOperatorValidator.class.php
r1413 r1444 55 55 * @since 0.11.0 56 56 */ 57 public function __construct(AgaviIValidatorContainer $parent, array $arguments, array $errors = array(), array $parameters = array() )57 public function __construct(AgaviIValidatorContainer $parent, array $arguments, array $errors = array(), array $parameters = array(), $name = '') 58 58 { 59 parent::__construct($parent, $arguments, $errors, $parameters );59 parent::__construct($parent, $arguments, $errors, $parameters, $name); 60 60 61 61 if($this->getParameter('skip_errors')) { … … 101 101 102 102 /** 103 * Submits an error to the error manager.104 *105 * The stuff in the parameter specified in $index is submitted to the106 * error manager. If there is no parameter with this name, then 'error'107 * is tryed as an parameter and if even this fails, the stuff in108 * $backupError is sent.109 *110 * @param string The name of the error parameter to fetch the message111 * from.112 * @param string An default error message to be used if the given error113 * has no message set.114 *115 * @author Uwe Mesecke <uwe@mesecke.net>116 * @since 0.11.0117 */118 protected function throwError($index = null, $backupError = null)119 {120 $error = $this->getErrorMessage($index, $backupError);121 122 // if no error msg was supplied rethrow the child errors123 if($error === null) {124 foreach($this->errors as $childError) {125 $this->parentContainer->reportError($childError[0], $childError[1]);126 }127 } else {128 if($this->hasParameter('translation_domain')) {129 $error = $this->getContext()->getTranslationManager()->_($error, $this->getParameter('translation_domain'));130 }131 132 $this->parentContainer->reportError($this, $error);133 }134 }135 136 /**137 * Reports an error to the parent container.138 *139 * @param AgaviValidator The validator where the error occured.140 * @param string An error message.141 *142 * @author Dominik del Bondio <ddb@bitxtender.com>143 * @since 0.11.0144 * @see AgaviIValidatorContainer::reportError145 */146 public function reportError(AgaviValidator $validator, $errorMsg)147 {148 $this->errors[] = array($validator, $errorMsg);149 }150 151 /**152 103 * Adds new child validator. 153 104 * … … 175 126 $this->addChild($validator); 176 127 } 177 }178 179 /**180 * Gets the request from the parent.181 *182 * @return AgaviRequest The parent's request.183 *184 * @author Uwe Mesecke <uwe@mesecke.net>185 * @since 0.11.0186 */187 public function getRequest()188 {189 return $this->parentContainer->getRequest();190 128 } 191 129 -
branches/0.11/src/validator/AgaviValidator.class.php
r1422 r1444 43 43 { 44 44 /** 45 * validator field success flag 46 */ 47 const NOT_PROCESSED = -1; 48 49 /** 45 50 * validator error severity (the validator succeeded) 46 51 */ … … 107 112 108 113 /** 109 * @var array The field names which have been validated by this110 * validator111 */112 protected $validatedFieldnames = array();113 114 /**115 114 * @var array The name of the request parameters serving as argument to 116 115 * this validator. … … 124 123 125 124 /** 126 * @var string The last error index. When the validator throws an error 127 * the index which was thrown is stored here. 128 */ 129 protected $lastErrorIndex = null; 125 * @var AgaviValidationIncident The current incident. 126 */ 127 protected $incident = null; 130 128 131 129 /** … … 382 380 * Set means anything but empty string. 383 381 * 382 * @param bool Whether an error should be thrown for each missing 383 * argument if this validator is required. 384 * 384 385 * @return bool Whether the arguments are set. 385 386 * … … 387 388 * @since 0.11.0 388 389 */ 389 protected function hasAllArgumentsSet() 390 { 390 protected function checkAllArgumentsSet($throwError = true) 391 { 392 $isRequired = $this->getParameter('required', true); 393 $result = true; 394 391 395 $array = $this->validationParameters->getParameters(); 392 396 $baseParts = $this->curBase->getParts(); … … 395 399 $pName = $this->curBase->pushRetNew($argument)->__toString(); 396 400 if(!$this->validationParameters->hasParameter($pName) || $this->validationParameters->getParameter($pName) === "") { 397 return false; 398 } 399 } 400 return true; 401 if($throwError && $isRequired) { 402 $this->throwError(null, $pName); 403 } 404 $result = false; 405 } 406 } 407 return $result; 401 408 } 402 409 … … 432 439 * Submits an error to the error manager. 433 440 * 434 * The stuff in the parameter specified in $index is submitted to the 435 * error manager. If there is no parameter with this name, then 'error' 436 * is tryed as an parameter and if even this fails, the stuff in 437 * $backupError is sent. 441 * Will look up the index in the errors array with automatic fallback to the 442 * default error. You can optionally specify the fields affected by this 443 * error. The error will be appended to the current incident. 438 444 * 439 445 * @param string The name of the error parameter to fetch the message 440 446 * from. 441 * @param string An default error message to be used if the given error 442 * has no message set. 443 * 444 * @author Uwe Mesecke <uwe@mesecke.net> 445 * @since 0.11.0 446 */ 447 protected function throwError($index = null, $backupError = null) 448 { 449 $this->lastErrorIndex = $index; 450 451 $error = $this->getErrorMessage($index, $backupError); 447 * @param string|array The arguments which are affected by this error. 448 * If null is given it will affect all fields 449 * 450 * @author Dominik del Bondio <ddb@bitxtender.com> 451 * @since 0.11.0 452 */ 453 protected function throwError($index = null, $affectedArgument = null) 454 { 455 if($affectedArgument === null) { 456 $affectedArguments = $this->getFullArgumentNames(); 457 } else { 458 $affectedArguments = (array) $affectedArgument; 459 } 460 461 $error = $this->getErrorMessage($index); 452 462 453 463 if($this->hasParameter('translation_domain')) { … … 455 465 } 456 466 457 $this->reportError($this, $error); 458 } 459 460 461 /** 462 * Reports an error to the parent container. 463 * 464 * @param AgaviValidator The validator where the error occured. 465 * @param string An error message. 466 * 467 * @author Dominik del Bondio <ddb@bitxtender.com> 468 * @since 0.11.0 469 * @see AgaviIValidatorContainer::reportError 470 */ 471 public function reportError(AgaviValidator $validator, $errorMsg) 472 { 473 if(self::mapErrorCode($this->getParameter('severity', 'error')) > self::NONE) { 474 $this->parentContainer->reportError($validator, $errorMsg); 475 } 476 } 477 478 /** 479 * Returns a list of input fields that are per default affected by a failure 480 * of the validator 481 * 482 * The list consists of the fields in the parameters that are lists in 483 * affectedFieldNames and the space seperated list of fields in the 484 * parameter 'affects'. 485 * 486 * @return array The list of fields that are affected by an error. 487 * 488 * @author Uwe Mesecke <uwe@mesecke.net> 489 * @since 0.11.0 490 */ 491 public function getAffectedFields() 492 { 493 $fields = array(); 494 $base = $this->curBase->__toString(); 495 496 if($this->hasParameter('affects')) { 497 $f = array_map('trim', explode(' ', trim($this->getParameter('affects')))); 498 foreach($f as $n) { 499 if(!strlen($n)) { 500 continue; 501 } 502 $fields[] = $n; 503 } 504 } 505 506 $fields = array_merge($fields, $this->validatedFieldnames); 507 // filter out empty strings 508 $fields = array_filter($fields, 'strlen'); 509 510 return array_unique($fields); 511 } 467 if(!$this->incident) { 468 $this->incident = new AgaviValidationIncident($this, self::mapErrorCode($this->getParameter('severity', 'error'))); 469 } 470 471 $this->incident->addError(new AgaviValidationError($error, $index, $affectedArguments)); 472 } 473 512 474 513 475 /** … … 560 522 } 561 523 562 $fieldNames = array(); 563 foreach($this->getArguments() as $argument) { 564 $name = $this->curBase->pushRetNew($argument)->__toString(); 565 $fieldNames[] = $name; 566 $this->validatedFieldnames[] = $name; 567 } 524 $fieldnames = $this->getFullArgumentNames(); 568 525 569 526 $result = self::SUCCESS; 570 527 $errorCode = self::mapErrorCode($this->getParameter('severity', 'error')); 571 528 572 if($this-> hasAllArgumentsSet()) {529 if($this->checkAllArgumentsSet(false)) { 573 530 if(!$this->validate()) { 574 531 // validation failed, exit with configured error code … … 580 537 $result = $errorCode; 581 538 } else { 582 // no reason to throw any error since it wouldn't be included anyways 583 $result = self::NONE; 539 // we don't throw an error here because this is not an incident per se 540 // but rather a non validated field 541 $result = self::NOT_PROCESSED; 584 542 } 585 543 } 586 544 587 545 $vm = $this->getContext()->getValidatorManager(); 588 foreach($fieldNames as $fieldName) { 589 $vm->addFieldResult($this, $fieldName, $result, $this->lastErrorIndex); 546 foreach($fieldnames as $fieldname) { 547 $vm->addFieldResult($this, $fieldname, $result); 548 } 549 550 if($this->incident) { 551 $vm->addIncident($this->incident); 552 $this->incident = null; 590 553 } 591 554 … … 722 685 } 723 686 687 /** 688 * Returns all arguments with their full path. 689 * 690
