Search code examples
c#asp.net-mvcclassrequiredfieldvalidator

MVC Required validation attribute in different circumstances


Let's suppose we have a class Client:

  public class Client
  {
    public int ID { get; set; }
    public int Type { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string County { get; set; }
    public string Town { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }

    public string StudentName { get; set; }
    public string Birthdate { get; set; }

    public string Company { get; set; }

    public string Observations { get; set; }
   }

The type (field Type) of the client can be 1(person) or 2(company).

How can I add different required attributes for both cases?

I want for the first case (person) to have required attributes for the following fields: ID , TYPE, Name, Address and Email.

For the second case (company) I want to add required attributes for: ID , TYPE, StudentName, Address,Company and Email.

How can I do this?


Solution

  • There is a library called "MVC Foolproof Validation" for this: http://foolproof.codeplex.com/