0

Current project:

  • ASP.NET 4.5.1
  • MVC 5

I need to know if I can nest When() statements like this:

When(x => x.Cond1 == val1,
  () => {
    When(x => x.SubCond1 == SubVal1,
      () => {
        When(x => x.Final1 == finalVal1,
          () => {
            RuleFor(x => x.Field1)
              .NotEmpty().WithMessage("Should not be empty");
            // a few more here
          });
        When(x => x.Final2 == finalVal2,
          () => {
            RuleFor(x => x.Field8)
              .NotEmpty().WithMessage("Should not be empty");
            // a few more here
          });
      });
    When(x => x.SubCond2 == SubVal2,
      () => {
        RuleFor(x => x.Field16)
          .NotEmpty().WithMessage("Should not be empty");
        // a few more here
      });
  });

Because the last thing I want is to decorate 30+ form fields like this:

RuleFor(x => x.Field1)
  .NotEmpty().WithMessage("Should not be empty")
  .When(x => x.Cond1 == val)
  .When(x => x.SubCond1 == SubVal1)
  .When(x => x.Final1 == finalVal1);

That is just untenable.

None of the conditions require validation themselves, as none of them are actually user-editable fields (just user-selectable values); I just need to compare them against known values. I would use an if/else statement if that was actually more appropriate, which it isn’t.

The model is largely flat, with only the second-level When() representing an imported model, and the third-level being different ways to handle specific fields within the imported model.

René Kåbis
  • 842
  • 2
  • 9
  • 28

1 Answers1

1

You can't, but you can try and use Rule Sets to group your rules together. Also maybe check Cascade Mode.

Tarek
  • 1,219
  • 13
  • 18
  • Turns out I was constructing both my models and my validation wrong. Cleaned up both, and now everything is working well again. My problem was that my form was a chimera - it had content from two different primary models that were conditionally on the page depending on which role the user had, and then for each role there was an Address model brought in anywhere from one to three times, and it itself was also conditional on whether a different address was needed for each purpose. I abstracted out all the models, and did the same for the validation. – René Kåbis Aug 02 '16 at 19:04
  • In the end, the only `When(x => x.cond1 == val1, () => {});` I needed was for a subsection of the Address model where a user was presented with country/state&zip/province&postal options that needed to be handled just right. – René Kåbis Aug 02 '16 at 19:07
  • I would put that as a win. – Tarek Aug 02 '16 at 19:09
  • Indeedy. In fact, if I have time I might even write an article about the entire process, to help others in my situation. What made my case “special” is that there were two groups of users with zero overlap, so I gave each its own role (to make rights validation easy), but needed to have a single sign-up form (kinda easy) followed by - and this was the fuckup - a single profile creation page for both. Something that, if abandoned after registration, prevented the users from getting into their account until they completed the profile. I took the easy route first and it bit me in the arse. – René Kåbis Aug 02 '16 at 19:16
  • Oh now I see how that was a problem. Maybe you should write that article after all. – Tarek Aug 02 '16 at 19:34