0

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?

2
  • FluentValidation is a nice library for complex (and simple) validation : fluentvalidation.codeplex.com You would have a Rulefor(m => m.SudentName).NotEmpty().When(m => m.Type == <TypeCompany>); for example. Commented Apr 16, 2014 at 12:58
  • The class Client should be a base class. You should create Person and Company classes which derives from base class. Commented Apr 16, 2014 at 13:07

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

This could be something like this:

class Client
{
    [Required]
    public int ID { get; set; }
    [Required]
    public string Address { get; set; }
    [Required]
    public string Email { get; set; }
    public string Country { get; set; }
    public string Town { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string Birthdate { get; set; }
    public string Observations { get; set; }
}

class Person : Client
{
    [Required]
    public string Name { get; set; }
}

class Company : Client
{
    [Required]
    public string StudentName { get; set; }
    [Required]
    public string Company { get; set; }

}

Btw you answered yourself: "I want person", "I want company" - this states that you want 2 different classes for this not a single one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.