Currently I'm working on a MVC 3 Environment and I need to add a Validation that validates 2 Fields in Dataannotation Models just like the validation of [Required(ErrorMessage="Required!")]
that is triggered onsubmit.
Currently , The Validation [Required]
is working but i need to check if the bool
and dateTime
Datatype
Heres the Code in my Model
public class NameInfo: IValidatableObject
{
[Display(Name = "Name")]
[Required(ErrorMessage = "Required!")]
public string Name{ get; set; }
[Display(Name = "Start Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<DateTime> StartDate { get; set; }
[Display(Name = "Already Starting")]
public bool IsStarting{ get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (!IsStarting&& !StartDate.HasValue)
{
yield return new ValidationResult("Required.", new[] { "StartDate" });
}
}
}
Currently im using IValidatableObject
but its not validating unless the name
Field modelstate is already Valid
Thanks in Advance!