using System; using System.Collections; using log4net; using Spring.Validation; using System.ComponentModel.DataAnnotations; using Sleis.Utility; namespace Sleis.Validation.Spring { public class NullableDecimalValidator : RequiredValidator { public string Provider { get; set; } public string MessageId { get; set; } internal static ILog Log; public NullableDecimalValidator() { Log = LogManager.GetLogger(this.GetType()); } public override bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors) { Log.DebugFormat("Validate({0},{1},{2})", validationContext, contextParams, errors); bool result = true; IValidationContextProvider contextProvider = validationContext as IValidationContextProvider; if (EvaluateWhen(validationContext, contextParams)) { Double? nullableDecimal = base.EvaluateTest(validationContext, contextParams).ConvertTo(); if (!nullableDecimal.HasValue) { errors.AddError(Provider, new ErrorMessage(MessageId, contextProvider.ValidationContxt)); result = false; } } return result; } } }