I have a model that has a datetime property and I want to make sure that in the view, the form can't be submitted unless that editor for has a value.
employee {
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Required] // <- this isn't doing anything for me???
public DateTime DateOfBirth {get;set;}
}
is there an annotation I can use for this or do I have to use javascript in the page?
or is there another solution?
Update -
When I clear out the date editor, I get the following in my editor box:
mm/dd/yyyy
when I submit this, does this count as null or what? Making the DateTime property nullable didn't fix my issue, theres no validation taking place when I submit a form that has mm/dd/yyyy for the date
Your problem is that DateTime
always has a value.
You'll need to make it a nullable DateTime
:
[Required]
public DateTime? DateOfBirth { get; set; }
Now your property will be null when no value is present and your Required
attribute will behave as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With