I know there's data annotation to validate data such as [Required], [Range()] and so on. Not to mention the unobtrusive validation that makes it easier in the client side. Many thanks to them.
But what about if you need to validate the value that depends on the entity's property value? I have this scenarion, say:
My models:
public class Schedule
{
public int Id { get; set; }
public DatetimeOffset StartDate { get; set; }
public DatetimeOffset EndDate { get; set; }
}
Now in a form,
<input type="text" name="StartDate" />
<input type="text" name="EndDate" />
How would you validate that the EndDate should not be less than the StartDate? Is there a built-in attribute in data annotation for that? Or should make a custom one? It would be great if it would make use of the unobstrusive validation by microsoft.
Here's another scenario:
What if you would do validation that depends on the value that is saved in the db? Say,
public class Bag
{
//constructor
public int Capacity { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
public class Item
{
//constructor
public string Description { get; set; }
public virtual ICollection<Bag> Bags { get; set; }
}
That if you would do validation on the Items being added to the Bag but if the user tries to input beyond the limit of the Bag's Capacity, should show the validation error.
Is this possible?
I'm currently using ASP.NET MVC 4. EF5 Code first approach.