Add 'when' condition to the Reference Validator (SPRNET-1370)

This commit is contained in:
bbaia
2010-09-22 09:26:05 +00:00
parent 2086abb67d
commit bc3e560ad3
4 changed files with 81 additions and 11 deletions

View File

@@ -341,6 +341,7 @@ namespace Spring.Validation.Config
string typeName = "Spring.Validation.ValidatorReference, Spring.Core";
string name = GetAttributeValue(element, ValidatorDefinitionConstants.ReferenceNameAttribute);
string context = GetAttributeValue(element, ValidatorDefinitionConstants.ReferenceContextAttribute);
string when = GetAttributeValue(element, ValidatorDefinitionConstants.WhenAttribute);
MutablePropertyValues properties = new MutablePropertyValues();
properties.Add("Name", name);
@@ -348,6 +349,10 @@ namespace Spring.Validation.Config
{
properties.Add("Context", context);
}
if (StringUtils.HasText(when))
{
properties.Add("When", when);
}
IConfigurableObjectDefinition reference =
parserContext.ReaderContext.ObjectDefinitionFactory.CreateObjectDefinition(typeName, null, parserContext.ReaderContext.Reader.Domain);

View File

@@ -107,6 +107,11 @@
<xs:documentation><![CDATA[A SpEL expression evaluating to the context to switch to when evaluating the referenced validator.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="when" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that determines if this validator shall be executed.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="baseValidatorType">

View File

@@ -22,6 +22,7 @@ using System.Collections;
using Spring.Expressions;
using Spring.Objects.Factory;
using System;
namespace Spring.Validation
{
@@ -47,6 +48,7 @@ namespace Spring.Validation
private string name;
private IExpression context;
private IExpression when;
private IValidator validator;
#endregion
@@ -54,11 +56,32 @@ namespace Spring.Validation
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ValidatorReference"/> class.
/// Creates a new instance of the <see cref="ValidatorReference"/> class.
/// </summary>
public ValidatorReference()
{}
/// <summary>
/// Creates a new instance of the <see cref="ValidatorReference"/> class.
/// </summary>
/// <param name="when">
/// The expression that determines if this validator should be evaluated.
/// </param>
public ValidatorReference(string when)
: this((when != null ? Expression.Parse(when) : null))
{}
/// <summary>
/// Creates a new instance of the <see cref="ValidatorReference"/> class.
/// </summary>
/// <param name="when">
/// The expression that determines if this validator should be evaluated.
/// </param>
public ValidatorReference(IExpression when)
{
this.when = when;
}
#endregion
#region Properties
@@ -83,6 +106,16 @@ namespace Spring.Validation
set { context = value; }
}
/// <summary>
/// Gets or sets the expression that determines if this validator should be evaluated.
/// </summary>
/// <value>The expression that determines if this validator should be evaluated.</value>
public IExpression When
{
get { return when; }
set { when = value; }
}
#endregion
/// <summary>
@@ -105,17 +138,23 @@ namespace Spring.Validation
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
public bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors)
{
if (Context != null)
bool valid = true;
if (When == null ||
Convert.ToBoolean(When.GetValue(validationContext, contextParams)))
{
validationContext = Context.GetValue(validationContext, contextParams);
if (Context != null)
{
validationContext = Context.GetValue(validationContext, contextParams);
}
if (validator == null)
{
validator = (IValidator)objectFactory.GetObject(Name);
}
valid = validator.Validate(validationContext, contextParams, errors);
}
if (validator == null)
{
validator = (IValidator) objectFactory.GetObject(Name);
}
return validator.Validate(validationContext, contextParams, errors);
return valid;
}
/// <summary>

View File

@@ -40,7 +40,7 @@ namespace Spring.Validation
StaticListableObjectFactory factory = new StaticListableObjectFactory();
factory.AddObject("validator", new TrueValidator());
ValidatorReference v = new ValidatorReference();
ValidatorReference v = new ValidatorReference("true");
v.ObjectFactory = factory;
v.Name = "validator";
@@ -65,6 +65,21 @@ namespace Spring.Validation
Assert.IsFalse(v.Validate(null, errors));
}
[Test]
public void FalseValidatorReferenceNotEvaluatedBecauseWhenExpressionReturnsFalse()
{
StaticListableObjectFactory factory = new StaticListableObjectFactory();
factory.AddObject("validator", new FalseValidator());
ValidatorReference v = new ValidatorReference("false");
v.ObjectFactory = factory;
v.Name = "validator";
IValidationErrors errors = new ValidationErrors();
Assert.IsTrue(v.Validate(null, null, errors));
Assert.IsTrue(v.Validate(null, errors));
}
[Test]
public void ContextNarrowing()
{
@@ -93,7 +108,13 @@ namespace Spring.Validation
Assert.IsTrue(v2.Validate(context, null, errors));
Assert.IsTrue(v2.Validate(context, errors));
}
ValidatorReference v3 = new ValidatorReference("false");
v3.ObjectFactory = factory;
v3.Name = "cv2";
v3.Context = Expression.Parse("DOB");
Assert.IsTrue(v3.Validate(null, errors));
}
}
}