0

I am using the Html.ValidationMessageFor() method to display validation messages on the form. For most of the form fields, I am getting the appropriate error message, except for a couple of fields. One is a 'date of birth' field which is a textbox on the form, and the other is a 'state' field, which is dropdown with all the US states. For these two fields, I get the 'The DateOfBirth field is required.' and 'The State field is required.'

I fired up reflector and looked at the code in the Html.ValidationMessageFor() static methods, and it didn't seem like those methods were adding this message.

Any ideas where this message could be coming from & how to fix it?

UPDATE Added model code ... Basically I am using a trimmed down version of CSLA. The business rules are added to the class when it is created, and fired once, the appropriate method is called. Basically, 'rule.ErrorMessage' is what should show up when the ValidationMessageFor lists out the errors. It works for all other properties, except the DateofBirth & State properties.

Model Code

Properties:

public Guid UserId { get; set; }
public Guid PatientId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone1 { get; set; }
public string Phone2 { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public States State { get; set; }
public string Zip { get; set; }
public DateTime DateOfBirth { get; set; }
public string EmergencyName { get; set; }
public string EmergencyPhone { get; set; }
public string EmergencyRelation { get; set; }

Business Rules:

this.AddValidation(new DataValidation("DateOfBirth", "DateOfBirthRequired",
   delegate(DataValidation rule)
   {
      rule.ErrorMessage = "required";
      return ((DateOfBirth >= new DateTime(1900, 1, 1)) && (DateOfBirth < DateTime.Today));
   }, DataAction.Update | DataAction.Insert));
this.AddValidation(new DataValidation("State", "StateRequired",
   delegate(DataValidation rule)
   {
      rule.ErrorMessage = "required";
      return (State != States.Unknown);
   }, DataAction.Insert | DataAction.Update));
Skadoosh
  • 2,575
  • 8
  • 40
  • 53

1 Answers1

0

All I needed to do was to set the properties as nullable as mentioned in this post.

Community
  • 1
  • 1
Skadoosh
  • 2,575
  • 8
  • 40
  • 53