1

I have a model class with several properties like below and each property have Data Annotations for the validation

Class User{

[Required]
public string First Name{get;set;}

[Required]
public string Last Name {get;set;}

[Required, EmailAddress, MaxLength(256), Display(Name = "Email Address")]
public string Email {get;set;}

[Required, MaxLength(20), DataType(DataType.Password), Display(Name ="Password")]
public string Pasword {get;set;}
}

Now, in the Console app I ask user to give me an email address and password to Login. How can I validate if a given email and password satify the conditions. Since it is login, I dont want to validate First Name and Last Name

var context = new ValidationContext(user);
var results = new List<ValidationResult>();

var isValid = Validator.TryValidateObject(user, context, results, true);

TryValidateObject takes an instance as a first parameter, so when I do this it also returns me error for First Name and Last Name

Can someone please suggest how can I validate only Email and password using the same User class?

2 Answers 2

1

If you have a different part of the app that requires a different set or properties and validation rules, then you should be using a completely distinct model for that purpose. So you could have a new model like this:

public class UserCredentials
{
    [Required, EmailAddress, MaxLength(256), Display(Name = "Email Address")]
    public string Email {get;set;}

    [Required, MaxLength(20), DataType(DataType.Password), Display(Name ="Password")]
    public string Password {get;set;}
}

Now your code that looks something like this will work:

UserCredentials userCredentials = .... //Get the credentials from somewhere

var context = new ValidationContext(userCredentials);
var results = new List<ValidationResult>();

var isValid = Validator.TryValidateObject(userCredentials, context, results, true);

Also, if you want to share this code with the user class, you could make use of inheritance:

public class User : UserCredentials
{
    [Required]
    public string FirstName{ get; set; }

    [Required]
    public string LastName { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi David, Thank you for the quick reply! One more question : Now when I validate against User class, it first validates FirstName, LastName and then properties of UserCredentials. Is there any way we can change the order as we want?
1

I would split the class up into a base class for the user name and password and the create a class off of it to hold the user name and password.

Something like this:

Class User : AuthUser {

    [Required]
    public string First Name{get;set;}

    [Required]
    public string Last Name {get;set;}

}

Class AuthUser {
    [Required, EmailAddress, MaxLength(256), Display(Name = "Email Address")]
    public string Email {get;set;}

    [Required, MaxLength(20), DataType(DataType.Password), Display(Name ="Password")]
    public string Pasword {get;set;}
}

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.