FluentValidation: Validation message not showing (ASP.Net core + Autofac + FluentValidation)
Hi there, I am working on ASP.Net Core application in which I want to do validation for the viewmodel. I cannot use [Validator] attribute on my viewmodel as they are in different assembly which I cannot reference to my Core assembly. My validator does work but when it returns the view back, it’s not showing any validation messages. I have all the jquery libraries configured and included, in fact it was working with data annotation attributes (ie. Required, EmailAddress etc.) and showing messages in the browser. Here is the code:
- WebApp.Core.dll
public class UserRegistrationViewModel
{
public string BusinessUnitName { get; set; }
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
public string PrimaryUserEmail { get; set; }
public string PrimaryUserName { get; set; }
}
- WebApp.Business.dll
public sealed class UserRegistrationViewModelValidator :
AbstractValidator<UserRegistrationViewModel>
{
public UserRegistrationViewModelValidator(IEntitiesContext context)
{
RuleFor(vm => vm.BusinessUnitName)
.NotEmpty();
RuleFor(vm => vm.ConfirmPassword)
.NotEmpty()
.Equal(vm => vm.Password)
.WithMessage(LocalizedStrings.ConfirmPasswordCompareErrorMessage);
RuleFor(vm => vm.Password)
.NotEmpty()
.Length(6, 25);
RuleFor(vm => vm.PrimaryUserEmail)
.NotEmpty()
.EmailAddress();
RuleFor(vm => vm.PrimaryUserName)
.NotEmpty()
.Length(6, 50);
}
}
- WebApp.dll (Startup.cs)
services
.AddMvc()
.AddFluentValidation(fv =>
fv.RegisterValidatorsFromAssemblyContaining<UserRegistrationViewModelValidator>());
- WebApp.dll (UserController.cs)
[AllowAnonymous]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> Registration(UserRegistrationViewModel viewModel)
{
if (!ModelState.IsValid)
{
return View(viewModel);
}
string UserUrl = Request.GetRootUrl();
var result = await _mediator.Send(new InsertQuery<UserRegistrationDto>
{
Dto = new UserRegistrationDto
{
BusinessUnitName = viewModel.BusinessUnitName,
Email = viewModel.PrimaryUserEmail,
Name = viewModel.PrimaryUserName,
Password = viewModel.Password,
Url = UserUrl
}
});
if (!result.Succeed)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error);
}
return View(viewModel);
}
var appUser = await _userManager.FindByEmailAsync(viewModel.PrimaryUserEmail).ConfigureAwait(false);
await _signInManager.SignInAsync(appUser, true).ConfigureAwait(false);
return RedirectToAction("Index", "Home");
}
I am not sure what is missing, I couldn’t find any example that shows the implementations I am looking for. Please note that I am using Autofac instead of default asp.not core IoC.
Thanks, Binoy
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (9 by maintainers)
So this doesn’t look right to me:
…that registers the validators with MVC’s built-in standard container. If you’re using autofac then I’d expect to see you registering a custom ValidatorFactory with FV that calls into autofac instead.
Have you already got an AutofactValidatorFactory written?