I have Custom Unique Email validation using ASP.NET Core-6 Web API Entity Framework project
public class UserUniqueEmailValidator : ValidationAttribute
{
private readonly ApplicationDbContext _dbContext;
public UserUniqueEmailValidator(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public bool IsUniqueUserEmailValidator(string email)
{
if (_dbContext.ApplicationUsers.SingleOrDefault(x => x.Email.ToLower() == email.ToLower()) == null) return true;
return false;
}
}
Then I called it here:
[Required(ErrorMessage = "Email field is required. ERROR!")]
[StringLength(100, ErrorMessage = "Email field must not exceed 100 characters.")]
[EmailAddress(ErrorMessage = "The Email field is not a valid e-mail address.")]
[UserUniqueEmailValidator(ErrorMessage = "Email already exists !!!")]
public string Email { get; set; }
But I got this error:
There is no argument given that corresponds to the required formal parameter 'dbContext' of 'UserUniqueEmailValidator.UserUniqueEmailValidator(ApplicationDbContext)'
How do I resolve it?
Thanks