renamed ShortcircuitEvaluate to FastValidate
added FastValidate semantics to all ValidateGroup-derived classes
This commit is contained in:
@@ -47,21 +47,27 @@ namespace Spring.Validation
|
||||
/// Initializes a new instance of the <see cref="AnyValidatorGroup"/> class.
|
||||
/// </summary>
|
||||
public AnyValidatorGroup()
|
||||
{}
|
||||
{
|
||||
this.FastValidate = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AnyValidatorGroup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
|
||||
public AnyValidatorGroup(string when) : base(when)
|
||||
{}
|
||||
{
|
||||
this.FastValidate = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AnyValidatorGroup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
|
||||
public AnyValidatorGroup(IExpression when) : base(when)
|
||||
{}
|
||||
{
|
||||
this.FastValidate = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -72,30 +78,23 @@ namespace Spring.Validation
|
||||
/// <param name="contextParams">Additional context parameters.</param>
|
||||
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
|
||||
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
/// </summary>
|
||||
/// <remarks>This is just an alias for <see cref="ValidatorGroup.FastValidate"/></remarks>
|
||||
public bool ValidateAll
|
||||
{
|
||||
get { return validateAll; }
|
||||
set { validateAll = value; }
|
||||
get { return !base.FastValidate; }
|
||||
set { base.FastValidate = !value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,7 +89,7 @@ namespace Spring.Validation
|
||||
/// all error messages returned by the item validators</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Actual implementation how to validate the specified object.
|
||||
/// </summary>
|
||||
/// <param name="validationContext">The object to validate.</param>
|
||||
/// <param name="contextParams">Additional context parameters.</param>
|
||||
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
|
||||
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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).
|
||||
/// </p>
|
||||
/// <p>
|
||||
/// By default, this validator group uses <c><see cref="ValidatorGroup.FastValidate"/> == true</c> semantics.
|
||||
/// </p>
|
||||
/// </remarks>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
public class ExclusiveValidatorGroup : ValidatorGroup
|
||||
@@ -46,57 +49,60 @@ namespace Spring.Validation
|
||||
/// Initializes a new instance of the <see cref="ExclusiveValidatorGroup"/> class.
|
||||
/// </summary>
|
||||
public ExclusiveValidatorGroup()
|
||||
{}
|
||||
{
|
||||
this.FastValidate = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ExclusiveValidatorGroup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
|
||||
public ExclusiveValidatorGroup(string when) : base(when)
|
||||
{}
|
||||
{
|
||||
this.FastValidate = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ExclusiveValidatorGroup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
|
||||
public ExclusiveValidatorGroup(IExpression when) : base(when)
|
||||
{}
|
||||
{
|
||||
this.FastValidate = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Validates the specified object.
|
||||
/// Actual implementation how to validate the specified object.
|
||||
/// </summary>
|
||||
/// <param name="validationContext">The object to validate.</param>
|
||||
/// <param name="contextParams">Additional context parameters.</param>
|
||||
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
|
||||
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
/// </remarks>
|
||||
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
|
||||
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Actual implementation how to validate the specified object.
|
||||
/// </summary>
|
||||
/// <param name="validationContext">The object to validate.</param>
|
||||
/// <param name="contextParams">Additional context parameters.</param>
|
||||
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
|
||||
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user