Problem Creating Custom Validator on Collection properties
Net MVC 3 for development. Now there is a scenario where i need to create to a custom validator on a collection property. Now the problem I am facing is that this custom validator works well when the length of the collection is 1 but when there are more than one elements in the collection i face a problem is the custom validation pushes errors but the problem occurs that when checked in ModelState the error is not found plugged. So in this case what should i do ? I am pasting in the code as below.
This is the Model i want to validate:
public class CreateTestModel
{
[Required]
public string Name { get; set; }
public string Description { get; set; }
public RadioButtonListViewModel<GoalTypes> Goals { get; set; }
[MinimumRequired("IncludedEntities", "ExcludedEntities", 1, ErrorMessage = "1 Entity is compulsory")]
public IEnumerable<TestEntityModel> IncludedEntities { get; set; }
public IEnumerable<TestEntityModel> ExcludedEntities { get; set; }
public IEnumerable<TestFilterModel> IncludedFilters { get; set; }
public IEnumerable<TestFilterModel> ExcludedFilters { get; set; }
public IEnumerable<BucketModel> Buckets { get; set; }
public bool AutoDecision { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int AdminId { get; set; }
[DefaultValue(true)]
public bool IsEnabled { get; set; }
}
Purpose of the Custom Validator:- I need to validate minimum number of required entities in a collection. There are some scenarios when for eg in this case : The IncludedEntities and the ExcludedEntities needs to have a minimum count of 1 including both the properties ,this is my business rule. So for this the Below custom Validator is written.
This is the Custom Validator i have written :
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class MinimumRequired : ValidationAttribute,IClientValidatable
{
public int numberOfMandatoryEntities{get; private set;}
public int totalCountofIncludeEntities { get; private set; }
public bool isBucket { get; set; }
public string Property1{get; private set;}
public string Property2{ get; private set; }
private const string DefaultErrorMessageFormatString = "Atleast one entity is required";
public MinimumRequired(string Property1, string Property2, int MandatoryCount)
{
this.Property1 = Property1;
if (!String.IsNullOrEmpty(Property2))
this.Property2 = Property2;
numberOfMandatoryEntities = MandatoryCount;
}
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, name);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
object property2Value = null;
object property1Value = null;
int property1Count=0;
if(!String.IsNullOrEmpty(Property2))
property2Value = validationContext.ObjectInstance.GetType().GetProperty(Property2).GetValue(validationContext.ObjectInstance, null);
property1Value = validationContext.ObjectInstance.GetType().GetProperty(Property1).GetValue(validationContext.ObjectInstance, null);
if (property1Value != null)
开发者_如何学Python {
property1Count = ((IEnumerable<Object>)property1Value).Count();
}
if (property2Value != null)
{
property1Count = property1Count + ((IEnumerable<Object>)property2Value).Count();
}
if (property1Count < numberOfMandatoryEntities)
{
return new ValidationResult(ErrorMessage);
}
return ValidationResult.Success;
}
#region IClientValidatable Members
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var x = new ModelClientValidationRule();
x.ValidationType = "entityvalidator";
x.ErrorMessage = string.Format(ErrorMessageString, metadata.GetDisplayName());
x.ValidationParameters.Add("mandatoryentity", numberOfMandatoryEntities);
return new[]
{
x
};
}
#endregion
}
Please help me out...
I triend the above code it works perfectly for me.
精彩评论