using System; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using log4net; using Sleis.Models; using Sleis.Utility; using Sleis.ViewModels; using Spring.Validation; namespace Sleis.Validation.Spring { public class FieldValidator : BaseValidator { public SleisViewType ViewType { get; set; } public string IdPrefix { get; set; } public object Test { get; set; } internal static ILog Log; public FieldValidator() { Log = LogManager.GetLogger(this.GetType()); } public override string ToString() { return ReflectionUtility.GetPublicPropertiesString(this); } public override bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors) { Log.DebugFormat("Validate({0},{1},{2})", validationContext, contextParams, errors); List fields = validationContext as List; if (fields == null) { throw new InvalidCastException("Validated object is not of List type"); } for (int i = 0; i < fields.Count; i++) { CustomFieldView fieldView = fields[i]; string fieldViewArgText = null; if (String.IsNullOrEmpty(fieldView.ValidationContxt)) { fieldViewArgText = fieldView.Field.Label; } else { fieldViewArgText = fieldView.ValidationContxt; } object[] fieldViewArgs = new object[] { fieldViewArgText }; if (fieldView.Field == null || fieldView.Value == null) { throw new ArgumentOutOfRangeException("CustomFieldView not properly configured"); } //Public Required if (ViewType == SleisViewType.Public) { if (fieldView.Field.PublicStyle == CustomFieldDataStyleType.Required && fieldView.ValueIsNullOrEmpty) { errors.AddError(GetMemberName(i), new ErrorMessage(IdPrefix + ".required", fieldViewArgs)); continue; } }//Public Required //Agency Required else if (ViewType == SleisViewType.Agency) { if (fieldView.Field.ManagementStyle == CustomFieldDataStyleType.Required && fieldView.ValueIsNullOrEmpty) { errors.AddError(GetMemberName(i), new ErrorMessage(IdPrefix + ".required", fieldViewArgs)); continue; } }//Agency Required //If it has data, make sure it is of the right type if (!fieldView.ValueIsNullOrEmpty) { switch (fieldView.Field.Type) { case CustomFieldDataType.Boolean: if (fieldView.Value.FieldValue.Value != "Y" && fieldView.Value.FieldValue.Value != "N") { errors.AddError(GetMemberName(i), new ErrorMessage(IdPrefix + ".bool", fieldViewArgs)); continue; } break; case CustomFieldDataType.Date: DateTime testDate = DateTime.MinValue; if (!DateTime.TryParse(fieldView.Value.FieldValue.Value, out testDate)) { errors.AddError(GetMemberName(i), new ErrorMessage(IdPrefix + ".date", fieldViewArgs)); continue; } else { fieldView.Value.FieldValue.Value = testDate.ToString("yyyy-MM-dd"); } break; case CustomFieldDataType.Numeric: decimal testNumber = Decimal.MinValue; if (!Decimal.TryParse(fieldView.Value.FieldValue.Value, out testNumber)) { errors.AddError(GetMemberName(i), new ErrorMessage(IdPrefix + ".number", fieldViewArgs)); continue; } break; default: break; } //switch //If we are still in the game, let's check the regex //TODO: Check if the requirement for all RegEx to start with the pattern escape will work if (!String.IsNullOrEmpty(fieldView.Field.FormatExpression) && fieldView.Field.FormatExpression.StartsWith("^")) { Regex re = new Regex(fieldView.Field.FormatExpression, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); if (!re.IsMatch(fieldView.Value.FieldValue.Value)) { errors.AddError(GetMemberName(i), new ErrorMessage(IdPrefix + ".format", fieldViewArgs)); continue; } } }//empty }//for each return errors.IsEmpty; } private string GetMemberName(int i) { return String.Format("CustomFields[{0}]", i); } } }