skip to Main Content

I have a register form with fields such as: Email, username, password and etc. And have certain validations for all of them like for example, email must have @, username length must be at least 5 letters, password must match and etc.
At the moment, I set up an error message response which would show the error but what I want to do is show all the errors.
How can I go about doing this?

Here is my usermanagerresponse model:

public class UserManagerResponse
    {
        public string AccessToken { get; set; }

        public string ErrorMessage { get; set; }
        public bool IsSuccess { get; set; }
        public IEnumerable<Claim> Claims { get; set; }
      //public IEnumerable<string> Errors { get; set; } <--- should i use this?
        public DateTime? ExpireDate { get; set; }
    }

This is what happens when users click on the register button to register for an account:

public async Task<UserManagerResponse> RegisterUserAsync(RegisterRequest model)
{

        if (model == null)
           throw new NullReferenceException("Register Model is null");
        if (model.Password != model.ConfirmPassword)
                return new UserManagerResponse
                {
                    ErrorMessage = "The passwords you have entered do not match",
                    IsSuccess = false,
                };
        if (model.Password.length < 5) 
                return new UserManagerResponse
                {
                    ErrorMessage = "Password length must be greater than 5",
                    IsSuccess = false,
                };
                     

}

If a user passes into model something like… Password: "lol1" and then Confirm: "lool1"

I want my error messages to be:
The passwords you have entered do not match
Password length must be greater than 5

How I can achieve this?

Thank you!

2

Answers


  1. Validation of the view model can be done before any controller methods are hit by decorating the model with attributes. Also complexity is also verified by whatever has been configured in startup.cs. These errors should automatically be shown on the view, if you use the standard models

    public class RegisterViewModel
    {
        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    
        [Required]
        [DataType(DataType.Password)]
        [Compare("Password")]
        public string ConfirmPassword { get; set; }
    }
    
    Login or Signup to reply.
  2. Your problem is that you are returning as soon as you hit the first error, so the rest of your tests are never hit – you can only return once. What you can do is build the response as you go and only send it at the end:

    public async Task<UserManagerResponse> RegisterUserAsync(RegisterRequest model) {
    
        if (model == null) {
           throw new NullReferenceException("Register Model is null");
        }
    
        //create error message string and boolean to track errors here
        UserManagerResponse response = new UserManagerResponse{
            IsSuccess = true,
            ErrorMessage = ""
        };
     
        if (model.Password != model.ConfirmPassword) {
            response.ErrorMessage += "The passwords you have entered do not match. ";
            response.IsSuccess = false;
        }
    
        if (model.Password.Length < 5) {
            response.ErrorMessage += "Password length must be greater than 5. ");
            response.IsSuccess = false;
        }
    
        //any other tests here built the same way
    
        if (response.isSuccess){
            //success functions here
        }
    
        return response;
    }
    

    This way your output would be:

    The passwords you have entered do not match. Password length must be greater than 5.


    I would suggest having separate lines for each error message, it’s cleaner and easier to read, particularly if you get a lot of them. What you would do for that is create a new line on each added error:

    response.ErrorMessage += "The passwords you have entered do not match.n";
    

    And at the end before you return clean up the extra new line:

    response.ErrorMessage = response.ErrorMessage.Trim();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search