resolved SPRNET-1033, introduced new "ShortcircuitEvaluate" property on ValidatorGroup
This commit is contained in:
@@ -44,7 +44,7 @@ namespace Spring.Validation
|
||||
#region Fields
|
||||
|
||||
private IList validators = new ArrayList();
|
||||
|
||||
private bool shortcircuitEvaluate = false;
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
@@ -83,6 +83,19 @@ namespace Spring.Validation
|
||||
set { validators = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When set <c>true</c>, shortcircuits evaluation.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The validators within the group will only be validated
|
||||
/// in order until the first validator fails.
|
||||
/// </remarks>
|
||||
public bool ShortcircuitEvaluate
|
||||
{
|
||||
get { return shortcircuitEvaluate; }
|
||||
set { shortcircuitEvaluate = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
@@ -101,6 +114,10 @@ namespace Spring.Validation
|
||||
foreach (IValidator validator in validators)
|
||||
{
|
||||
valid = validator.Validate(validationContext, contextParams, errors) && valid;
|
||||
if (shortcircuitEvaluate && !valid)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
ProcessActions(valid, validationContext, contextParams, errors);
|
||||
}
|
||||
|
||||
@@ -56,6 +56,25 @@ namespace Spring.Validation
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAllValidatorsReturnFalseFast()
|
||||
{
|
||||
ValidatorGroup vg = new ValidatorGroup();
|
||||
vg.ShortcircuitEvaluate = true;
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsFalse(valid, "Validation should fail when all inner validators return false.");
|
||||
Assert.AreEqual(1, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAllValidatorsReturnTrue()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user