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));
		}
Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.