Conditional FluentValidation rule in RegisterValidator

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
I need to test for two conditions in the customer registration page.

Googling "FluentValidation rule for multiple properties" I found the easiest way to be the "When" rule; unfortunately I cannot get it to work in the RegisterValidator form.

EXAMPLE (after adding the "When" part to the original line of code it never shows the validation message)
RuleFor(x => x.LastName).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.LastName.Required")).When(x => string.IsNullOrEmpty(x.FirstName));

I've seen this post saying that "When" is not a supported FluentValidation rule in client-side validation:
https://www.nopcommerce.com/boards/t/32055/form-validation-problems.aspx

What is really baffling me are the similar pages in nopCommerce site that use "When", ex. BlogPostValidator, ProductReviewsValidator, NewsItemValidator and lots more in the Admin site.

LETS TAKE BlogPost AS AN EXAMPLE

BlogPost.cshtml
@Html.ValidationMessageFor(model => model.AddNewComment.CommentText)

BlogPostValidator.cs
RuleFor(x => x.AddNewComment.CommentText).NotEmpty().WithMessage(localizationService.GetResource("Blog.Comments.CommentText.Required")).When(x => x.AddNewComment != null);


BlogPost.cshtml and Register.cshtml are both pages in "Views" folders using "@Html.ValidationMessageFor(model => " with input field checking being handled by their respective validators.
Am I correct in assuming that they are both performing "client-side" validation as they contain forms which are run on the client? So why does "When" work in BlogPost and lots of other nopCommerce pages but not in "Register"?

Ultimately how can I check for two conditions in the RegisterValidator form?
Thanks
6 years ago
UPDATE

I have read previous posts to see if anyone else had the same problem and they have:

1)
https://www.nopcommerce.com/boards/t/28037/problem-with-extending-validation-in-registration-area.aspx
This member was trying to call a function in RegisterValidator to validate Bangladeshi mobile numbers but her function was not being called. No useful answers were given.

2)
https://www.nopcommerce.com/boards/t/20984/fluentvalidation-rule-not-working-as-expected.aspx
This member was also trying to call a function, this time using "Must" to ensure the user input was a numeric interger.
This time there was a useful answer from a savvy "a.m." team member, explaining that whenever JavaScript is being used it is "client-side" validation and the FluentValidation rules supported are limited. "When", "Must", custom functions ect cannot be used.
The workaround is to use the Controller instead of the Validator and insert your validation code before the (ModelState.IsValid) check.

I tried what he suggested, editing CustomerController -> ActionResult Register

This
RuleFor(x => x.LastName).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.LastName.Required")).When(x => string.IsNullOrEmpty(x.FirstName));

translates to
if (String.IsNullOrEmpty(model.FirstName) && String.IsNullOrEmpty(model.LastName))
{
ModelState.AddModelError("", _localizationService.GetResource("Account.Fields.LastName.Required"));
}

Ok so this workaround actually works, thanks a.m., but differently. It allows the form to be posted but instead of saving the data it comes back with an error at the top of the form, not alongside the LastName field, with the error message.

I'm still baffled why some pages manage to use "When" and am unable to spot the difference between the way Register and BlogPost submit data (both are using input type="submit" buttons) but thanks to this forum I now have a compromise solution. Sorry if I have written a long post. I am new to MVC and nopCommerce and hope to help anyone else who stumbles across the same problem!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.