I am in need of validating field against database possibly using other registered services. I have been reading documentation, but haven't found an actual answer.
There is remote validation that calls to controller method but it is client side only so disabling JS disables validation. There is also custom validation that makes use of validation attributes and is nicely contained within ModelState.IsValid
but I cannot inject dependencies into it's constructor.
I have found this question marked as answered as it speaks of dependency resolver but I cannot find any useful information about it.
I tried research it but all I came across was only about dependency injections or IServiceProvider
which didn't work for me. Here are some of my failed attempts:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//--1-------------------------------
var container = new ServiceContainer();
var _context = container.GetService<ApplicationDbContext>();
//----------------------------------
//--2-------------------------------
var services = new ServiceCollection();
IServiceProvider servicesProvider = services.BuildServiceProvider();
var _context = servicesProvider.GetService<ApplicationDbContext>();
//----------------------------------
return ValidationResult.Success;
}
Each retrieved _context was null. I have really exhausted all ideas in here and I will be grateful for any help.