How to access the FluentValidation ErrorCodes?

Joe 41 Reputation points
2023-05-15T20:43:13.3466667+00:00

I’m trying to access the FluentValidation ErrorCodes with the following code, which is hosted in my Controller.

My ModelState.IsValid is returning false, as I have errors, which is correct. The validation works ok on the View i.e. validation errors are being displayed.

But my validator doesn’t return any errors even when I have errors!

public class ApplicationValidator : AbstractValidator<ApplicationViewModel>
	{
		public ApplicationValidator()
		{
			RuleSet("EligibilityValidator", () =>
			{

				RuleFor(x => x.EligibilityRuleOne)
					.NotNull()
					.WithErrorCode("1")
					.WithMessage("Error message 1");

				RuleFor(x => x.EligibilityRuleTwo)
					.NotNull()
					.WithErrorCode("2")
					.WithMessage("Error message 2");

				RuleFor(x => x.EligibilityRuleThree)
					.NotNull()
					.WithErrorCode("3")
					.WithMessage("Error message 3");
			});
		}
	}
[HttpPost]
		[ValidateAntiForgeryToken]
		public IActionResult EligibilityChecks([CustomizeValidator(RuleSet = "EligibilityValidator")] ApplicationViewModel model, string SaveContinuebtn)
		{
			if (ModelState.IsValid)
			{
				if (!string.IsNullOrEmpty(SaveContinuebtn))
				{
					return RedirectToAction("Register");
				}
			}

		    var validator = new ApplicationValidator();
			var validationResult = validator.Validate(model);
			var errors = validationResult.Errors
				.OrderBy(e => e.ErrorCode)
				.ToList();
			foreach (var error in errors)
			{
				ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
			}

			return View(GetViewPath1("Index", model));
		}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,360 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,404 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,599 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,076 Reputation points Microsoft Vendor
    2023-05-17T10:17:53.44+00:00

    Hi @Joe

                var validator = new ApplicationValidator();
    			var validationResult = validator.Validate(model);
    

    The issue relates the Validate() method. Since you are setting the rule sets when define the validation rules, so when using the Validate() method, you need to set the RuleSets. If not set the RuleSets, the validation result is true.

    Try to modify your code as below:

    		    var validator = new ApplicationValidator();
    			var validationResult = validator.Validate(model, options => options.IncludeRuleSets("EligibilityValidator"));
    

    You can check this screenshot:

    image2


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments