Index: trunk/extensions/Validator/includes/ItemParameterCriterion.php |
— | — | @@ -87,15 +87,32 @@ |
88 | 88 | } |
89 | 89 | |
90 | 90 | if ( $result->hasInvalidItems() ) { |
| 91 | + $allInvalid = count( $result->getInvalidItems() ) == count( $parameter->value ); |
| 92 | + |
| 93 | + // If the parameter is required and all items are invalid, it's fatal. |
| 94 | + // Else it's high for required, and normal for non-required parameters. |
| 95 | + if ( $parameter->isRequired() ) { |
| 96 | + $severity = $allInvalid ? ValidationError::SEVERITY_FATAL : ValidationError::SEVERITY_HIGH; |
| 97 | + } |
| 98 | + else { |
| 99 | + $severity = $allInvalid ? ValidationError::SEVERITY_NORMAL : ValidationError::SEVERITY_LOW; |
| 100 | + } |
| 101 | + |
91 | 102 | $result->addError( |
92 | | - new ValidationError( $this->getListErrorMessage( $parameter, $result->getInvalidItems() ) ) |
| 103 | + new ValidationError( |
| 104 | + $this->getListErrorMessage( $parameter, $result->getInvalidItems() ), |
| 105 | + $severity |
| 106 | + ) |
93 | 107 | ); |
94 | 108 | } |
95 | 109 | } |
96 | 110 | else { |
97 | 111 | if ( !$this->doValidation( $parameter->value ) ) { |
98 | 112 | $result->addError( |
99 | | - new ValidationError( $this->getItemErrorMessage( $parameter ) ) |
| 113 | + new ValidationError( |
| 114 | + $this->getItemErrorMessage( $parameter ), |
| 115 | + $parameter->isRequired() ? ValidationError::SEVERITY_FATAL : ValidationError::SEVERITY_NORMAL |
| 116 | + ) |
100 | 117 | ); |
101 | 118 | } |
102 | 119 | } |
Index: trunk/extensions/Validator/includes/Parameter.php |
— | — | @@ -433,10 +433,9 @@ |
434 | 434 | |
435 | 435 | /** |
436 | 436 | * Validates the parameter value. |
| 437 | + * Also sets the value to the default when it's not set or invalid, assuming there is a default. |
437 | 438 | * |
438 | 439 | * @since 0.4 |
439 | | - * |
440 | | - * @return boolean Indicates if there was any validation error |
441 | 440 | */ |
442 | 441 | protected function doValidation() { |
443 | 442 | if ( $this->setCount == 0 ) { |
— | — | @@ -445,42 +444,35 @@ |
446 | 445 | throw new Exception( 'Attempted to validate a required parameter without first setting a value.' ); |
447 | 446 | } |
448 | 447 | else { |
449 | | - $success = true; |
450 | 448 | $this->setToDefault(); |
451 | 449 | } |
452 | 450 | } |
453 | 451 | else { |
454 | | - $success = $this->validateCriteria(); |
| 452 | + $this->validateCriteria(); |
| 453 | + |
| 454 | + if ( count( $this->errors ) > 0 && !$this->hasFatalError() ) { |
| 455 | + $this->setToDefault(); |
| 456 | + } |
455 | 457 | } |
456 | | - |
457 | | - return $success; |
458 | 458 | } |
459 | 459 | |
460 | 460 | /** |
461 | 461 | * Validates the provided value against all criteria. |
462 | 462 | * |
463 | 463 | * @since 0.4 |
464 | | - * |
465 | | - * @return boolean Indicates if there was any validation error |
466 | 464 | */ |
467 | 465 | protected function validateCriteria() { |
468 | | - $success = true; |
469 | | - |
470 | 466 | foreach ( $this->getCriteria() as $criterion ) { |
471 | 467 | $validationResult = $criterion->validate( $this ); |
472 | 468 | |
473 | 469 | if ( !$validationResult->isValid() ) { |
474 | | - $success = false; |
475 | | - |
476 | 470 | $this->handleValidationError( $validationResult ); |
477 | 471 | |
478 | | - if ( !self::$accumulateParameterErrors ) { |
479 | | - break; |
| 472 | + if ( !self::$accumulateParameterErrors || $this->hasFatalError() ) { |
| 473 | + break; |
480 | 474 | } |
481 | 475 | } |
482 | 476 | } |
483 | | - |
484 | | - return $success; |
485 | 477 | } |
486 | 478 | |
487 | 479 | /** |
— | — | @@ -753,4 +745,19 @@ |
754 | 746 | $this->applyManipulationsToDefault = $doOrDoNot; |
755 | 747 | } |
756 | 748 | |
| 749 | + /** |
| 750 | + * Returns false when there are no fatal errors or an ValidationError when one is found. |
| 751 | + * |
| 752 | + * @return mixed false or ValidationError |
| 753 | + */ |
| 754 | + public function hasFatalError() { |
| 755 | + foreach ( $this->errors as $error ) { |
| 756 | + if ( $error->isFatal() ) { |
| 757 | + return true; |
| 758 | + } |
| 759 | + } |
| 760 | + |
| 761 | + return false; |
| 762 | + } |
| 763 | + |
757 | 764 | } |
\ No newline at end of file |
Index: trunk/extensions/Validator/includes/Validator.php |
— | — | @@ -250,17 +250,20 @@ |
251 | 251 | $this->registerNewError( |
252 | 252 | wfMsgExt( 'validator_error_required_missing', 'parsemag', $paramName ), |
253 | 253 | array(), |
254 | | - ValidationError::SEVERITY_CRITICAL |
| 254 | + ValidationError::SEVERITY_FATAL |
255 | 255 | ); |
256 | 256 | break; |
257 | 257 | } |
258 | 258 | else { |
259 | | - $validationSucceeded = $parameter->validate(); |
| 259 | + $parameter->validate(); |
260 | 260 | |
261 | | - if ( !$validationSucceeded ) { |
262 | | - foreach ( $parameter->getErrors() as $error ) { |
263 | | - $this->registerError( $error ); |
264 | | - } |
| 261 | + foreach ( $parameter->getErrors() as $error ) { |
| 262 | + $this->registerError( $error ); |
| 263 | + } |
| 264 | + |
| 265 | + if ( $parameter->hasFatalError() ) { |
| 266 | + // If there was a fatal error, and the parameter is required, stop processing. |
| 267 | + break; |
265 | 268 | } |
266 | 269 | |
267 | 270 | $initialSet = $this->parameters; |
— | — | @@ -293,7 +296,7 @@ |
294 | 297 | if ( !array_key_exists( $paramName, $initialParamSet ) ) { |
295 | 298 | $this->paramsTohandle[] = $paramName; |
296 | 299 | } |
297 | | - } |
| 300 | + } |
298 | 301 | } |
299 | 302 | |
300 | 303 | $dependencies = array(); |
— | — | @@ -390,8 +393,6 @@ |
391 | 394 | * @return mixed false or ValidationError |
392 | 395 | */ |
393 | 396 | public function hasFatalError() { |
394 | | - global $egErrorLevel; |
395 | | - |
396 | 397 | foreach ( $this->errors as $error ) { |
397 | 398 | if ( $error->isFatal() ) { |
398 | 399 | return $error; |