Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Validation
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18842/src/Spring/Spring.Core/Validation
Added Files:
AnyValidatorGroup.cs BaseValidator.cs
CachingVariableResolver.cs ExclusiveValidatorGroup.cs
IValidator.cs IVariableResolver.cs
ObjectNavigatorVariableResolver.cs SimpleValidator.cs
ValidationErrors.cs ValidatorGroup.cs
Log Message:
Initial validation framework checkin.
--- NEW FILE: ValidationErrors.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System.Collections;
using Spring.Util;
namespace Spring.Validation
{
/// <summary>
/// Container for validation errors.
/// </summary>
/// <remarks>
/// <p>
/// This class groups validation errors by validator names and
/// allows access to both complete errors collection and
/// to errors for a certain validator.
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: ValidationErrors.cs,v 1.1 2005/11/17 00:34:14 aseovic Exp $</version>
public sealed class ValidationErrors
{
private IList errors = new ArrayList();
private IDictionary errorMap = new Hashtable();
/// <summary>
/// Gets a value indicating whether this instance is empty, meaning that there are no
/// validation errors.
/// </summary>
/// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value>
public bool IsEmpty
{
get { return errors.Count == 0; }
}
/// <summary>
/// Adds an error message to error collections.
/// </summary>
/// <param name="key">The key that should be used for message grouping; can be <c>null</c>.</param>
/// <param name="errorMessage">The error message to add.</param>
public void AddError(string key, string errorMessage)
{
AssertUtils.ArgumentNotNull(errorMessage, "errorMessage");
errors.Add(errorMessage);
if (key != null)
{
IList errList = (IList) errorMap[key];
if (errList == null)
{
errList = new ArrayList();
errorMap[key] = errList;
}
errList.Add(errorMessage);
}
}
/// <summary>
/// Merges another instance of <see cref="ValidationErrors"/> into this one.
/// </summary>
/// <param name="errorsToMerge">The validation errors to merge.</param>
public void MergeErrors(ValidationErrors errorsToMerge)
{
((ArrayList) this.errors).AddRange(errorsToMerge.errors);
foreach (DictionaryEntry de in errorsToMerge.errorMap)
{
ArrayList errList = (ArrayList) this.errorMap[de.Key];
if (errList == null)
{
this.errorMap[de.Key] = de.Value;
}
else
{
errList.AddRange((IList) de.Value);
}
}
}
/// <summary>
/// Gets the list of all errors.
/// </summary>
/// <returns>A list of all errors.</returns>
public IList GetErrors()
{
return errors;
}
/// <summary>
/// Gets the list of errors for the specified lookup key.
/// </summary>
/// <returns>
/// A list of all errors for the specified key, or <c>null</c>,
/// if there are no errors for the specified key.
/// </returns>
public IList GetErrors(string key)
{
return (IList) errorMap[key];
}
}
}
--- NEW FILE: IVariableResolver.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
namespace Spring.Validation
{
/// <summary>
/// Interface that has to be implemented by different variabl;e resolvers.
/// </summary>
/// <remarks>
/// <p>
/// Variable resolvers are used by the validation framework to resolve variable expressions
/// that can be used within validator condition, target and message parameters.
/// </p>
/// <p>
/// By default, variables will be resolved using
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: IVariableResolver.cs,v 1.3 2005/11/17 00:34:14 aseovic Exp $</version>
public interface IVariableResolver
{
/// <summary>
/// Resolves the specified variable.
/// </summary>
/// <param name="variable">Name of the variable.</param>
/// <returns></returns>
object Resolve(string variable);
}
}
--- NEW FILE: ValidatorGroup.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections;
using Spring.Context;
namespace Spring.Validation
{
/// <summary>
/// <see cref="IValidator"/> implementation that supports grouping of validators.
/// </summary>
/// <remarks>
/// <p>
/// This validator will be valid only when all of the validators in the <c>Validators</c>
/// collection are valid.
/// </p>
/// <p>
/// <c>ValidationErrors</c> property will return a union of all validation error messages
/// for the contained validators.
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: ValidatorGroup.cs,v 1.3 2005/11/17 00:34:14 aseovic Exp $</version>
public class ValidatorGroup : BaseValidator
{
private IList validators = new ArrayList();
/// <summary>
/// Initializes a new instance of the <see cref="ValidatorGroup"/> class.
/// </summary>
public ValidatorGroup()
{
}
/// <summary>
/// Gets or sets the validators.
/// </summary>
/// <value>The validators.</value>
public IList Validators
{
get { return validators; }
set { validators = value; }
}
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</param>
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
/// <param name="messageSource">Message source that should be used to resolve validation error messages.</param>
/// <param name="resolver">Variable resolver to use.</param>
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
public override bool Validate(object validationContext, ValidationErrors errors, IMessageSource messageSource, IVariableResolver resolver)
{
bool valid = true;
if (EvaluateCondition(When, resolver))
{
foreach (IValidator validator in validators)
{
valid = validator.Validate(validationContext, errors, messageSource, resolver) && valid;
}
if (!valid && Message != null)
{
errors.AddError(Name, ResolveErrorMessage(messageSource, resolver));
}
}
return valid;
}
/// <summary>
/// Doesn't do anything for validator group as there is no single test.
/// </summary>
/// <param name="test">The test to evaluate.</param>
/// <param name="resolver">Variable resolver to use.</param>
/// <returns><c>True</c> if specified test is valid, <c>False</c> otherwise.</returns>
protected override bool EvaluateTest(string test, IVariableResolver resolver)
{
throw new NotSupportedException("Validator group does not support this method.");
}
}
}
--- NEW FILE: BaseValidator.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using Spring.Context;
using Spring.Context.Support;
namespace Spring.Validation
{
/// <summary>
/// Base class that defines common properties for all validators.
/// </summary>
/// <remarks>
/// <p>
/// Custom validators should always extend this class instead of
/// simply implementing <see cref="IValidator"/> interface, in
/// order to inherit common validator functionality.
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: BaseValidator.cs,v 1.3 2005/11/17 00:34:14 aseovic Exp $</version>
public abstract class BaseValidator : IValidator
{
#region Instance Fields
private string name;
private string test;
private string message;
private string[] messageParams;
private string when;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="BaseValidator"/> class.
/// </summary>
public BaseValidator()
{}
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</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 bool Validate(object validationContext, ValidationErrors errors)
{
return Validate(validationContext, errors, new NullMessageSource());
}
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</param>
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
/// <param name="messageSource">Message source that should be used to resolve validation error messages.</param>
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
public virtual bool Validate(object validationContext, ValidationErrors errors, IMessageSource messageSource)
{
IVariableResolver resolver = new CachingVariableResolver(new ObjectNavigatorVariableResolver(validationContext));
return Validate(validationContext, errors, messageSource, resolver);
}
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</param>
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
/// <param name="messageSource">Message source that should be used to resolve validation error messages.</param>
/// <param name="resolver">Variable resolver to use.</param>
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
public virtual bool Validate(object validationContext, ValidationErrors errors, IMessageSource messageSource, IVariableResolver resolver)
{
bool valid = true;
if (EvaluateCondition(when, resolver))
{
valid = EvaluateTest(test, resolver);
if (!valid)
{
errors.AddError(Name, ResolveErrorMessage(messageSource, resolver));
}
}
return valid;
}
/// <summary>
/// Evaluates the test.
/// </summary>
/// <param name="test">The test to validate.</param>
/// <param name="resolver">Variable resolver to use during evaluation.</param>
/// <returns><c>True</c> if specified object is valid, <c>False</c> otherwise.</returns>
protected abstract bool EvaluateTest(string test, IVariableResolver resolver);
#region Properties
/// <summary>
/// Gets or sets the validator name.
/// </summary>
/// <value>The name.</value>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// Gets or sets the validation test.
/// </summary>
/// <value>The validation test.</value>
public string Test
{
get { return test; }
set { test = value; }
}
/// <summary>
/// Gets or sets the resource name of the validation error message.
/// </summary>
/// <value>The resource name of the validation error message.</value>
public string Message
{
get { return message; }
set { message = value; }
}
/// <summary>
/// Gets or sets the message params.
/// </summary>
/// <remarks>
/// Message parameters will be resolved against the validation context
/// and passed as the parameters to
/// </remarks>
/// <value>The message params.</value>
public string[] MessageParams
{
get { return messageParams; }
set { messageParams = value; }
}
/// <summary>
/// Gets or sets the condition for validator evaluation.
/// </summary>
/// <remarks>
/// <p>
/// This property allows user to specify a logical condition that will
/// determine whether validator should be evaluated or not.
/// </p>
/// <p>
/// Any variables referenced within the condition expression will be resolved
/// against validation context using configured instance of <see cref="IVariableResolver"/>.
/// </p>
/// </remarks>
/// <value>The condition for validator evaluation.</value>
public string When
{
get { return when; }
set { when = value; }
}
#endregion
#region Helper Methods
/// <summary>
/// Evaluates a logical condition.
/// </summary>
/// <param name="condition">Condition to evaluate.</param>
/// <param name="resolver">Variable resolver to use during evaluation.</param>
/// <returns><c>True</c> if the condition is valid, <c>False</c> otherwise.</returns>
protected bool EvaluateCondition(string condition, IVariableResolver resolver)
{
if (condition == null)
{
return true;
}
// TODO: implement condition evaluation
return true;
}
/// <summary>
/// Resolves the error message.
/// </summary>
/// <param name="messageSource">The message source.</param>
/// <param name="resolver">Variable resolver to use for message parameters resolution.</param>
/// <returns>Resolved error message</returns>
protected string ResolveErrorMessage(IMessageSource messageSource, IVariableResolver resolver)
{
if (messageParams != null && messageParams.Length > 0)
{
object[] parameters = ResolveMessageParameters(resolver);
return messageSource.GetMessage(message, parameters);
}
else
{
return messageSource.GetMessage(message);
}
}
/// <summary>
/// Resolves the message parameters.
/// </summary>
/// <param name="resolver">Variable resolver to use.</param>
/// <returns>Resolved message parameters.</returns>
protected object[] ResolveMessageParameters(IVariableResolver resolver)
{
object[] parameters = new object[messageParams.Length];
for (int i = 0; i < messageParams.Length; i++)
{
parameters[i] = resolver.Resolve(messageParams[i]);
}
return parameters;
}
#endregion
}
}
--- NEW FILE: ObjectNavigatorVariableResolver.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using Spring.Objects.Navigation;
namespace Spring.Validation
{
/// <summary>
/// Implementation of <see cref="IVariableResolver"/> that uses
/// <see cref="ObjectNavigator"/> to resolve variables.
/// </summary>
/// <remarks>
/// <p>
/// This is default implementation of the <see cref="IVariableResolver"/>
/// that will be used by the validation framework unless user configures
/// validators to use different implementation.
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: ObjectNavigatorVariableResolver.cs,v 1.3 2005/11/17 00:34:14 aseovic Exp $</version>
internal class ObjectNavigatorVariableResolver : IVariableResolver
{
private object context;
/// <summary>
/// Initializes a new instance of the <see cref="ObjectNavigatorVariableResolver"/> class.
/// </summary>
/// <param name="context">The context to resolve variable against.</param>
public ObjectNavigatorVariableResolver(object context)
{
this.context = context;
}
/// <summary>
/// Resolves the specified variable.
/// </summary>
/// <param name="variable">Name of the variable.</param>
/// <returns></returns>
public object Resolve(string variable)
{
return ObjectNavigator.GetValue(context, variable);
}
}
}
--- NEW FILE: CachingVariableResolver.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System.Collections;
namespace Spring.Validation
{
/// <summary>
/// Implementation of <see cref="IVariableResolver"/> that
/// caches values of resolved variables.
/// </summary>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: CachingVariableResolver.cs,v 1.1 2005/11/17 00:34:14 aseovic Exp $</version>
internal class CachingVariableResolver : IVariableResolver
{
private IDictionary cache = new Hashtable();
private IVariableResolver resolver;
/// <summary>
/// Initializes a new instance of the <see cref="CachingVariableResolver"/> class.
/// </summary>
/// <param name="resolver">The actual resolver to delegate variable resolution to.</param>
public CachingVariableResolver(IVariableResolver resolver)
{
this.resolver = resolver;
}
/// <summary>
/// Resolves the specified variable.
/// </summary>
/// <param name="variable">Name of the variable.</param>
/// <returns></returns>
public object Resolve(string variable)
{
if (cache.Contains(variable))
{
return cache[variable];
}
object res = resolver.Resolve(variable);
cache[variable] = res;
return res;
}
}
}
--- NEW FILE: IValidator.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using Spring.Context;
namespace Spring.Validation
{
/// <summary>
/// Interface that has to be implemented by different validators.
/// </summary>
/// <remarks>
/// <p>
/// If you are implementing validators you will probably never have to implement
/// this interface directly, unless you want to change the behavior of the validation
/// framework.
/// </p>
/// <p>
/// Instead, you should derive your validators from <see cref="BaseValidator"/> class
/// and implement asbtract <c>ValidateTarget</c> method.
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: IValidator.cs,v 1.3 2005/11/17 00:34:14 aseovic Exp $</version>
public interface IValidator
{
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</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>
bool Validate(object validationContext, ValidationErrors errors);
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</param>
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
/// <param name="messageSource">Message source that should be used to resolve validation error messages.</param>
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
bool Validate(object validationContext, ValidationErrors errors, IMessageSource messageSource);
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</param>
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
/// <param name="messageSource">Message source that should be used to resolve validation error messages.</param>
/// <param name="resolver">Variable resolver to use.</param>
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
bool Validate(object validationContext, ValidationErrors errors, IMessageSource messageSource, IVariableResolver resolver);
}
}
--- NEW FILE: SimpleValidator.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
namespace Spring.Validation
{
/// <summary>
/// Evaluates validator test using condition evaluator.
/// </summary>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: SimpleValidator.cs,v 1.1 2005/11/17 00:34:14 aseovic Exp $</version>
public abstract class SimpleValidator : BaseValidator
{
/// <summary>
/// Initializes a new instance of the <see cref="ConditionValidator"/> class.
/// </summary>
public SimpleValidator()
{}
/// <summary>
/// Evaluates the test using condition evaluator.
/// </summary>
/// <remarks>
/// Test can be any logical expression that is supported by Spring.NET logical
/// expression evaluation engine, and can use any variables that can be resolved
/// by the variable resolver used by the validation engine.
/// </remarks>
/// <param name="test">The test to evaluate.</param>
/// <param name="resolver">Variable resolver to use.</param>
/// <returns><c>True</c> if specified test is valid, <c>False</c> otherwise.</returns>
protected override bool EvaluateTest(string test, IVariableResolver resolver)
{
object objectToValidate = resolver.Resolve(test);
return ValidateObject(objectToValidate);
}
/// <summary>
/// Validates the object.
/// </summary>
/// <param name="objectToValidate">The object to validate.</param>
/// <returns><c>True</c> if specified object is valid, <c>False</c> otherwise.</returns>
protected abstract bool ValidateObject(object objectToValidate);
}
}
--- NEW FILE: ExclusiveValidatorGroup.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using Spring.Context;
namespace Spring.Validation
{
/// <summary>
/// <see cref="IValidator"/> implementation that supports grouping of validators.
/// </summary>
/// <remarks>
/// <p>
/// This validator will be valid when <b>one or more</b> of the validators in the <c>Validators</c>
/// collection are valid.
/// </p>
/// <p>
/// <c>ValidationErrors</c> property will return a union of all validation error messages
/// for the contained validators, but only if this validator is not valid (meaning, when none
/// of the contained validators are valid).
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: ExclusiveValidatorGroup.cs,v 1.1 2005/11/17 00:34:14 aseovic Exp $</version>
public class ExclusiveValidatorGroup : ValidatorGroup
{
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveValidatorGroup"/> class.
/// </summary>
public ExclusiveValidatorGroup()
{}
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</param>
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
/// <param name="messageSource">Message source that should be used to resolve validation error messages.</param>
/// <param name="resolver">Variable resolver to use.</param>
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
public override bool Validate(object validationContext, ValidationErrors errors, IMessageSource messageSource, IVariableResolver resolver)
{
ValidationErrors tmpErrors = new ValidationErrors();
bool valid = false;
if (EvaluateCondition(When, resolver))
{
foreach (IValidator validator in Validators)
{
bool tmpValid = validator.Validate(validationContext, tmpErrors, messageSource, resolver);
if (valid && tmpValid)
{
valid = false;
break;
}
else if (tmpValid)
{
valid = true;
}
}
if (!valid)
{
errors.AddError(Name, ResolveErrorMessage(messageSource, resolver));
}
}
return valid;
}
}
}
--- NEW FILE: AnyValidatorGroup.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using Spring.Context;
namespace Spring.Validation
{
/// <summary>
/// <see cref="IValidator"/> implementation that supports grouping of validators.
/// </summary>
/// <remarks>
/// <p>
/// This validator will be valid when <b>one or more</b> of the validators in the <c>Validators</c>
/// collection are valid.
/// </p>
/// <p>
/// <c>ValidationErrors</c> property will return a union of all validation error messages
/// for the contained validators, but only if this validator is not valid (meaning, when none
/// of the contained validators are valid).
/// </p>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <version>$Id: AnyValidatorGroup.cs,v 1.3 2005/11/17 00:34:14 aseovic Exp $</version>
public class AnyValidatorGroup : ValidatorGroup
{
/// <summary>
/// Initializes a new instance of the <see cref="AnyValidatorGroup"/> class.
/// </summary>
public AnyValidatorGroup()
{}
/// <summary>
/// Validates the specified object.
/// </summary>
/// <param name="validationContext">The object to validate.</param>
/// <param name="errors"><see cref="ValidationErrors"/> instance to add error messages to.</param>
/// <param name="messageSource">Message source that should be used to resolve validation error messages.</param>
/// <param name="resolver">Variable resolver to use.</param>
/// <returns><c>True</c> if validation was successful, <c>False</c> otherwise.</returns>
public override bool Validate(object validationContext, ValidationErrors errors, IMessageSource messageSource, IVariableResolver resolver)
{
ValidationErrors tmpErrors = new ValidationErrors();
bool valid = false;
if (EvaluateCondition(When, resolver))
{
foreach (IValidator validator in Validators)
{
valid = validator.Validate(validationContext, tmpErrors, messageSource, resolver) || valid;
if (valid)
{
break;
}
}
if (!valid)
{
errors.MergeErrors(tmpErrors);
if (Message != null)
{
errors.AddError(Name, ResolveErrorMessage(messageSource, resolver));
}
}
}
return valid;
}
}
}
|