Lets say I have a StartDate and an EndDate and I wnt to check if the EndDate is not more then 3 months apart from the Start Date
public class DateCompare : ValidationAttribute
{
public String StartDate { get; set; }
public String EndDate { get; set; }
//Constructor to take in the property names that are supposed to be checked
public DateCompare(String startDate, String endDate)
{
StartDate = startDate;
EndDate = endDate;
}
public override bool IsValid(object value)
{
var str = value.ToString();
if (string.IsNullOrEmpty(str))
return true;
DateTime theEndDate = DateTime.ParseExact(EndDate, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
DateTime theStartDate = DateTime.ParseExact(StartDate, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).AddMonths(3);
return (DateTime.Compare(theStartDate, theEndDate) > 0);
}
}
and I would like to implement this into my validation
[DateCompare("StartDate", "EndDate", ErrorMessage = "The Deal can only be 3 months long!")]
I know I get an error here... but how can I do this sort of business rule validation in asp.net mvc
It's a late answer but I wanted to share it for others outhere. Here's how I've done it so that everything is validated using unobtrusive client validation:
Create an attribute class:
Note: if you want to validate length of time, add another parameter to the constractor and change enumerator for this specific type of comparsion
Add the attributes to the field as folows:
[DateCompareValidation(DateCompareValidationAttribute.CompareType.GreatherThenOrEqualTo, "This Date must be on or after another date", compareWith: "AnotherDate")]
Take a note how your generated html is changed. It should include your validation message, field name for the compare-to date, etc. The generated parms would start with "data-val-compare". You defined this "compare" when you set ValidationType="compare" in GetClientValidationRules method.
Now you need matching javascript code: to add an validation adapter and validation method. I used anonimous method here, but you don't have to. I recommend placing this code in a separate javascript file so that this file together with your attribute class become like a control and could be used anywhere.
This takes care only of client-side unobtrusive validation. If you need server side, you'd have to have some logic in the override of isValid method. Also, you can use Reflection to generate error message using display attributes, etc and make the message argument optional.
The Attribute
The Interface
The Validator
Register it:
Entity:
Attribute:
2. Password
Entity:
I hope it helps
Thanks for the info. I was left scratching my head when I wanted to bind the validation message to a property. If you switch the line
to...
you can move the compare above a specific property. Thanks for the info! Much help since my client is still running on 3.5 sp1. sad face
I have only figured out how to do this at the class level but not the at property level. If you create an MVC application, the Account model shows the approach illustrated below.
Class:
Validation Method: