Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Validator.TryValidateObject Issue

$
0
0

SOLVED. I was able to successfully override the error message using the ErrorMessage parameter of the Required attribute, e.g. [Required(ErrorMessage = "Please enter your email address.")].

I'm using Validator.TryValidateObject in a view model in a PCL for class:

public class Credentials : BaseModel
{
    [Required]
    public string Email { get; set; }
    [Required]
    public string Password { get; set; }

    public Credentials(string email, string password)
    {
        Email = email;
        Password = password;
    }
}

View Model:

var credentials = new Credentials(Email, Password);
var validationResults = new List<ValidationResult>();
var isValid = Validator.TryValidateObject(credentials, new ValidationContext(credentials), validationResults);

if (!isValid)
{
    foreach (var result in validationResults)
    {
        ValidationErrorMessage += string.Format("{0}\n", result.ErrorMessage);
    }
    ValidationError = true;
    return;
}

However, result.ErrorMessage is "The field Credentials is invalid." instead of "The Email field is required." or "The Password field is required." Am I doing something wrong?

Thanks.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>