EnVisageOnline/Main/Source/EnVisage/Models/ValidationAttributes/Scenarios/ValidationGroupAttribute.cs

111 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace EnVisage.Models.ValidationAttributes.Scenarios
{
[Obsolete("Seems like it is unused")]
public class ValidationGroupAttribute : ValidationAttribute, IClientValidatable
{
//private readonly string[] _properties;
private string _groupName;
private string _errorMessage;
private string _validationgroup;
private string _dependantProperty;
public ValidationGroupAttribute(string groupName, string errorMessage, string ValidationGroupProperty, string dependantProperty = "")//(params string[] properties)
{
_groupName = groupName;
_errorMessage = errorMessage;
_validationgroup = ValidationGroupProperty;
_dependantProperty = dependantProperty;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (string.IsNullOrEmpty(_groupName))
return null;
var validationGroup = validationContext.ObjectType.GetProperty("ValidationGroup");
if (validationGroup == null)
return null;
string currentGroup = validationGroup.GetValue(validationContext.ObjectInstance, null) as string;
if (!string.IsNullOrEmpty(currentGroup) && currentGroup.Equals(_groupName, StringComparison.InvariantCultureIgnoreCase))
{
if (validationContext.MemberName.Equals("EndDate"))
{
var validatedProperty = validationContext.ObjectType.GetProperty(validationContext.MemberName);
var startDateProperty = validationContext.ObjectType.GetProperty("StartDate");
var projectDeadlineProperty = validationContext.ObjectType.GetProperty("ProjectDeadline");
DateTime? endDate = (DateTime?)validatedProperty.GetValue(validationContext.ObjectInstance, null);
DateTime? startDate = (DateTime?)startDateProperty.GetValue(validationContext.ObjectInstance, null);
DateTime? projectDeadline = (DateTime?)projectDeadlineProperty.GetValue(validationContext.ObjectInstance, null);
//[DateGreaterThanOrEqualAttribute("StartDate", "Scenario End Date should not be less than Start Date")]
if (endDate.HasValue && startDate.HasValue && endDate.Value < startDate.Value)
{
return new ValidationResult("Scenario End Date should not be less than Start Date");
}
//[DateNotExceedPropValue("ProjectDeadline", "Scenario End Date should not exceed Project Deadline date")]
if (endDate.HasValue && projectDeadline.HasValue && endDate.Value > projectDeadline.Value)
{
return new ValidationResult("Scenario End Date should not exceed Project Deadline date");
}
}
}
//foreach (var property in _properties)
//{
//var propertyInfo = validationContext.ObjectType.GetProperty("group");
//if (propertyInfo == null)
//{
// return new ValidationResult(string.Format("unknown property {0}", "group"));
//}
//var propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
//if (propertyValue is string && !string.IsNullOrEmpty(propertyValue as string))
//{
// return null;
//}
//if (propertyValue != null)
//{
// return null;
//}
//}
//return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
return null;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = _errorMessage,
ValidationType = "validationgroup"
};
rule.ValidationParameters["group"] = _groupName;
if (!string.IsNullOrEmpty(_dependantProperty))
rule.ValidationParameters["dependantproperty"] = _dependantProperty;
var parentType = context.Controller.ViewData.Model.GetType();
var parentMetaData = ModelMetadataProviders.Current
.GetMetadataForProperties(context.Controller.ViewData.Model, parentType);
var otherValue = (string)parentMetaData.FirstOrDefault(p =>
p.PropertyName == _validationgroup).Model;
rule.ValidationParameters["currentgroup"] = otherValue;
//rule.ErrorMessage = _errorMessage;
yield return rule;
}
}
}