4

I'd like to make 2 rules using Fluent Validation (http://fluentvalidation.codeplex.com) in my MVC project.

Nothing should happen when both Company and Name are empty. If either of them is filled, nothing should happen either. If either Company or Name is not filled, display a label at both of them. (the error message could be the same)

Ive tried this so far:

RuleFor(x => x.Name)
.NotEmpty()
.WithMessage("Please fill in the company name or the customer name")
.Unless(x => !string.IsNullOrWhiteSpace(x.Company));

RuleFor(x => x.Company)
.NotEmpty()
.WithMessage("Please fill in the company name or the customer name")
.Unless(x => !string.IsNullOrWhiteSpace(x.Name));

Ive tried combinations of When, Must and Unless, but none of them work. When i fill in nothing, no errors are shown on those 2 properties.

Can anyone help me out?

Robin
  • 527
  • 5
  • 16
  • So nobody knows how to fix this? :( – Robin Nov 05 '12 at 12:07
  • 1
    I'm not able to reproduce you problem; I've run a simple test using your exact rules and they seem to be fine (i.e. just fail when both Name and Company are null). Could the issue be in integrating FluentValidation with ASP.NET MVC, i.e. does any of your other rules work? – Serge Belov Nov 08 '12 at 22:12
  • Well this works under normal cirucumstances, but i shouldve let you guys know that this form is being posted with ajax (jquery validate). I noticed certain rules such as When, Unless and Must are executed server side and therefor cannot validate on the client side without a total page postback. Certain rules such as NotEmpty() do work on the client side, but When, Unless and Must seems to be needing a page postback to validate. – Robin Nov 09 '12 at 09:38
  • @Robin - Are the inputs in the form being appended to the DOM after the page loads? – Travis J Nov 11 '12 at 20:18
  • @TravisJ yes, that is the case and the server side validation is being json serialized to the webpage on form submit. – Robin Nov 12 '12 at 08:38

2 Answers2

2

From the comments it seems the issue is enabling the client-side validation and the rules actually work on post-back. As specified in the FluentValidation wiki, the only supported client-side rules are

  • NotNull/NotEmpty
  • Matches (regex)
  • InclusiveBetween (range)
  • CreditCard
  • Email
  • EqualTo (cross-property equality comparison)
  • Length

so essentially what you're trying to achieve is not supported out-of-the-box.

Please have a look at an example of custom client-side FluentValidation rules here.

Community
  • 1
  • 1
Serge Belov
  • 5,633
  • 1
  • 31
  • 40
0

You could add two rules with when condition:

RuleFor(x => x.Name)
.NotEmpty()
.When(x => string.IsNullOrEmpty(x.Company))
.WithMessage("Please fill in the company name or the customer name");

RuleFor(x => x.Company)
.NotEmpty()
.When(x => string.IsNullOrEmpty(x.Name))
.WithMessage("Please fill in the company name or the customer name");
Artem Vyshniakov
  • 16,355
  • 3
  • 43
  • 47
  • Hi, thanks for the answer but i shouldve been more clear. I tried this approach and it doesnt work for some reason. – Robin Oct 31 '12 at 13:41