Reputation: 478
Even after removing the required attribute why am i getting "The date of Birth is required" Error message? Is it because of any other attributes? How can i change it? I am just checking that incase if some one puts up unusual age, i can find it.
namespace ProjectCrux.Models
{
public class Student
{
public int studentId { get; set; }
[Display(Name = "Date of Birth")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
[DateOfBirth(MinAge = 15, MaxAge = 90, ErrorMessage = "Too young or too old???")]
public DateTime dateOfBirth { get; set; }
public string salt { get; set; }
}
/*
* Attribute to validate date of birth within a range
*/
public class DateOfBirthAttribute : ValidationAttribute
{
public int MinAge { get; set; }
public int MaxAge { get; set; }
public override bool IsValid(object value)
{
if (value == null)
return true;
var val = (DateTime)value;
if (val.AddYears(MinAge) > DateTime.Now)
return false;
return (val.AddYears(MaxAge) > DateTime.Now);
}
}
}
Upvotes: 2
Views: 227
Reputation: 17680
You can make it nullable.
public DateTime? dateOfBirth { get; set; }
Upvotes: 2