'Custom Validation not firing, build-in does
I cant get my Custom Validator to fire in ASP.NET Core MVC.
I try to use a validator to check if the domain of the input e-mail is known
public class AllowedDomain : ValidationAttribute
{
public AllowedDomain() : base()
{
}
public string GetErrorMessage() => $"Some Error Message";
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
AllowedDomainRepository repo = (AllowedDomainRepository)validationContext
.GetService(typeof(AllowedDomainRepository));
string domain = value as string;
if (repo.hasDomain(domain))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult(GetErrorMessage());
}
}
}
And I use it like this in my RegisterModel:
[BindProperty]
public InputModel Input { get; set; }
....
public class InputModel
{
[Required]
[EmailAddress]
[AllowedDomain]
[Display(Name = "Email")]
public string Email { get; set; }
....
}
but somehow the [EmailAdress] attribute is working, as is the [Required] but I can't even get mine to fire (it's not reaching a breakpoint in the isValid method) even though Required and mine both inherit from ValidationAttribute.
EDIT: I got it to fire, once I post, but this is too late.
[EmailAdress] does fire while the input field looses focus which is what I want for my validation, too.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
