Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
[Required(ErrorMessage = "Confirmed is required")]
       [Display(Name = "Confirmed")]
       public Boolean Confirmed { get; set; }
       
       [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]       
       //[Required(ErrorMessage = "Confirmed Date is required")]
       [Display(Name = "Confirmed Date")]
       public DateTime ConfirmedDate { get; set; }


Above are two fields in my Model.
I just want to validate the ConfirmedDate when Confirmed CheckBox is checked, when it is unchecked
the ConfiredDate is not required.

Can anyone give an example or suggestion ?
Posted

1 solution

The typical way to accomplish this would be to execute the validation in the setter for Confirmed:
C#
private Boolean _Confirmed;
[Required(ErrorMessage = "Confirmed is required")]
[Display(Name = "Confirmed")]
public Boolean Confirmed
{
  get { return _Confirmed; } 
  set
  {
    if (value != _Confirmed)
    {
      _Confirmed = value;
      if (_Confirmed)
      {
        // here you validate ConfirmedDate
      }
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900