diff --git a/src/Spring/Spring.Core/Validation/AnyValidatorGroup.cs b/src/Spring/Spring.Core/Validation/AnyValidatorGroup.cs index 81e05e82..267dc85e 100644 --- a/src/Spring/Spring.Core/Validation/AnyValidatorGroup.cs +++ b/src/Spring/Spring.Core/Validation/AnyValidatorGroup.cs @@ -47,21 +47,27 @@ namespace Spring.Validation /// Initializes a new instance of the class. /// public AnyValidatorGroup() - {} + { + this.FastValidate = true; + } /// /// Initializes a new instance of the class. /// /// The expression that determines if this validator should be evaluated. public AnyValidatorGroup(string when) : base(when) - {} + { + this.FastValidate = true; + } /// /// Initializes a new instance of the class. /// /// The expression that determines if this validator should be evaluated. public AnyValidatorGroup(IExpression when) : base(when) - {} + { + this.FastValidate = true; + } #endregion @@ -72,30 +78,23 @@ namespace Spring.Validation /// Additional context parameters. /// instance to add error messages to. /// True if validation was successful, False otherwise. - public override bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors) + protected override bool ValidateGroup(IDictionary contextParams, IValidationErrors errors, object validationContext) { ValidationErrors tmpErrors = new ValidationErrors(); - bool valid = true; - - if (EvaluateWhen(validationContext, contextParams)) + bool valid = false; + foreach (IValidator validator in Validators) { - valid = false; - foreach (IValidator validator in Validators) + valid = validator.Validate(validationContext, contextParams, tmpErrors) || valid; + if (valid && FastValidate) { - valid = validator.Validate(validationContext, contextParams, tmpErrors) || valid; - if (valid) - { - break; - } + break; } - - if (!valid) - { - errors.MergeErrors(tmpErrors); - } - ProcessActions(valid, validationContext, contextParams, errors); } + if (!valid) + { + errors.MergeErrors(tmpErrors); + } return valid; } } diff --git a/src/Spring/Spring.Core/Validation/CollectionValidator.cs b/src/Spring/Spring.Core/Validation/CollectionValidator.cs index 909e16a2..fda98e79 100644 --- a/src/Spring/Spring.Core/Validation/CollectionValidator.cs +++ b/src/Spring/Spring.Core/Validation/CollectionValidator.cs @@ -29,7 +29,6 @@ namespace Spring.Validation { #region Fields - private bool validateAll = false; private bool includeElementErrors = false; private IExpression context; @@ -41,10 +40,11 @@ namespace Spring.Validation /// Gets or sets the value that indicates whether to validate all elements of the collection /// regardless of the errors. /// + /// This is just an alias for public bool ValidateAll { - get { return validateAll; } - set { validateAll = value; } + get { return !base.FastValidate; } + set { base.FastValidate = !value; } } /// @@ -89,7 +89,7 @@ namespace Spring.Validation /// all error messages returned by the item validators public CollectionValidator(bool validateAll, bool includeElementErrors) { - this.validateAll = validateAll; + this.FastValidate = validateAll; this.includeElementErrors = includeElementErrors; } @@ -106,7 +106,7 @@ namespace Spring.Validation public CollectionValidator(IExpression when, bool validateAll, bool includeElementErrors) : base(when) { - this.validateAll = validateAll; + this.FastValidate = validateAll; this.includeElementErrors = includeElementErrors; } @@ -122,7 +122,7 @@ namespace Spring.Validation public CollectionValidator(string when, bool validateAll, bool includeElementErrors) : this((when != null ? Expression.Parse(when) : null), validateAll,includeElementErrors) { - this.validateAll = validateAll; + this.FastValidate = validateAll; } #endregion @@ -150,41 +150,47 @@ namespace Spring.Validation throw new ArgumentException("The type of the object for validation must be subtype of IEnumerable."); } + return base.Validate(validationContext, contextParams, errors); + } + + /// + /// Actual implementation how to validate the specified object. + /// + /// The object to validate. + /// Additional context parameters. + /// instance to add error messages to. + /// True if validation was successful, False otherwise. + protected override bool ValidateGroup(IDictionary contextParams, IValidationErrors errors, object validationContext) + { bool valid = true; + IEnumerable collectionToValidate = (validationContext is IDictionary + ? ((IDictionary) validationContext).Values + : (IEnumerable) validationContext); - if (EvaluateWhen(validationContext, contextParams)) - { - IEnumerable collectionToValidate = (validationContext is IDictionary - ? ((IDictionary) validationContext).Values - : (IEnumerable) validationContext); - - // decide whether to pass new validation errors collection - //(and discard error messages returned by the item validators) - // OR to pass validation errors collection that was passed to this method - //(and collect all error messages returned by the item validators) - IValidationErrors err = (includeElementErrors)? errors : new ValidationErrors(); + // decide whether to pass new validation errors collection + //(and discard error messages returned by the item validators) + // OR to pass validation errors collection that was passed to this method + //(and collect all error messages returned by the item validators) + IValidationErrors err = (includeElementErrors)? errors : new ValidationErrors(); - foreach (object objectToValidate in collectionToValidate) - { - foreach (IValidator validator in Validators) - { - valid = validator.Validate(objectToValidate, contextParams, err) && valid; - if (!valid && !validateAll) - { - break; - } - } - - if (!valid && !validateAll) + foreach (object objectToValidate in collectionToValidate) + { + foreach (IValidator validator in this.Validators) + { + valid = validator.Validate(objectToValidate, contextParams, err) && valid; + if (!valid && this.FastValidate) { break; } } - - ProcessActions(valid, validationContext, contextParams, errors); - } + if (!valid && this.FastValidate) + { + break; + } + } return valid; } + } } \ No newline at end of file diff --git a/src/Spring/Spring.Core/Validation/ExclusiveValidatorGroup.cs b/src/Spring/Spring.Core/Validation/ExclusiveValidatorGroup.cs index 1a1a2cc5..fc5431f6 100644 --- a/src/Spring/Spring.Core/Validation/ExclusiveValidatorGroup.cs +++ b/src/Spring/Spring.Core/Validation/ExclusiveValidatorGroup.cs @@ -36,6 +36,9 @@ namespace Spring.Validation /// for the contained validators, but only if this validator is not valid (meaning, when none /// of the contained validators are valid). ///

+ ///

+ /// By default, this validator group uses == true semantics. + ///

/// /// Aleksandar Seovic public class ExclusiveValidatorGroup : ValidatorGroup @@ -46,57 +49,60 @@ namespace Spring.Validation /// Initializes a new instance of the class. ///
public ExclusiveValidatorGroup() - {} + { + this.FastValidate = true; + } /// /// Initializes a new instance of the class. /// /// The expression that determines if this validator should be evaluated. public ExclusiveValidatorGroup(string when) : base(when) - {} + { + this.FastValidate = true; + } /// /// Initializes a new instance of the class. /// /// The expression that determines if this validator should be evaluated. public ExclusiveValidatorGroup(IExpression when) : base(when) - {} + { + this.FastValidate = true; + } #endregion /// - /// Validates the specified object. + /// Actual implementation how to validate the specified object. /// /// The object to validate. /// Additional context parameters. /// instance to add error messages to. /// True if validation was successful, False otherwise. - public override bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors) + protected override bool ValidateGroup(IDictionary contextParams, IValidationErrors errors, object validationContext) { IValidationErrors tmpErrors = new ValidationErrors(); - bool valid = true; - - if (EvaluateWhen(validationContext, contextParams)) + bool valid = false; + foreach (IValidator validator in Validators) { - valid = false; - foreach (IValidator validator in Validators) + bool tmpValid = validator.Validate(validationContext, contextParams, tmpErrors); + if (valid && tmpValid) { - bool tmpValid = validator.Validate(validationContext, contextParams, tmpErrors); - if (valid && tmpValid) + valid = false; + if (this.FastValidate) { - valid = false; break; } - else if (tmpValid) - { - valid = true; - } } - - ProcessActions(valid, validationContext, contextParams, errors); + else if (tmpValid) + { + valid = true; + } } - return valid; + return valid; } + } } \ No newline at end of file diff --git a/src/Spring/Spring.Core/Validation/ValidatorGroup.cs b/src/Spring/Spring.Core/Validation/ValidatorGroup.cs index fb763af9..84954954 100644 --- a/src/Spring/Spring.Core/Validation/ValidatorGroup.cs +++ b/src/Spring/Spring.Core/Validation/ValidatorGroup.cs @@ -44,7 +44,7 @@ namespace Spring.Validation #region Fields private IList validators = new ArrayList(); - private bool shortcircuitEvaluate = false; + private bool fastValidate = false; #endregion #region Constructors @@ -90,10 +90,10 @@ namespace Spring.Validation /// The validators within the group will only be validated /// in order until the first validator fails. /// - public bool ShortcircuitEvaluate + public bool FastValidate { - get { return shortcircuitEvaluate; } - set { shortcircuitEvaluate = value; } + get { return fastValidate; } + set { fastValidate = value; } } #endregion @@ -107,21 +107,34 @@ namespace Spring.Validation /// True if validation was successful, False otherwise. public override bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors) { - bool valid = true; - if (EvaluateWhen(validationContext, contextParams)) { - foreach (IValidator validator in validators) - { - valid = validator.Validate(validationContext, contextParams, errors) && valid; - if (shortcircuitEvaluate && !valid) - { - break; - } - } + bool valid = ValidateGroup(contextParams, errors, validationContext); ProcessActions(valid, validationContext, contextParams, errors); + return valid; } + return true; + } + + /// + /// Actual implementation how to validate the specified object. + /// + /// The object to validate. + /// Additional context parameters. + /// instance to add error messages to. + /// True if validation was successful, False otherwise. + protected virtual bool ValidateGroup(IDictionary contextParams, IValidationErrors errors, object validationContext) + { + bool valid = true; + foreach (IValidator validator in validators) + { + valid = validator.Validate(validationContext, contextParams, errors) && valid; + if (!valid && FastValidate) + { + break; + } + } return valid; } diff --git a/test/Spring/Spring.Core.Tests/Validation/ValidatorGroupTests.cs b/test/Spring/Spring.Core.Tests/Validation/ValidatorGroupTests.cs index d29a68e4..0f21b28d 100644 --- a/test/Spring/Spring.Core.Tests/Validation/ValidatorGroupTests.cs +++ b/test/Spring/Spring.Core.Tests/Validation/ValidatorGroupTests.cs @@ -60,7 +60,7 @@ namespace Spring.Validation public void WhenAllValidatorsReturnFalseFast() { ValidatorGroup vg = new ValidatorGroup(); - vg.ShortcircuitEvaluate = true; + vg.FastValidate = true; vg.Validators.Add(new FalseValidator()); vg.Validators.Add(new FalseValidator()); vg.Validators.Add(new FalseValidator());