1

I have a model, the model has 2 strings; foo and bar.

foo is what it is, it will be one of x amount of possible values (it's not an enum, though).

bar is entered by the user, so I want to validate it. But I want the validation to be driven by foo, and I want to use Remote validation (to do it remotely).

My suggestion:

Create a controller MyValidationController with an action that the remote validation attribute will point to. To this action, I'll send values of foo and bar.

The action will then redirect to the action that corresponds to foo. So, if foo is "MyValue", I will call RedirectToAction("MyValue"). And the MyValue action will validate accordingly.

Assuming I've found a way to ensure this action exists, my question is:

Am I reinventing the wheel here? Is there a feature in MVC that would allow me to validate with a rule driven by a property submitted in the same form?

JᴀʏMᴇᴇ
  • 2,361
  • 4
  • 22
  • 25
  • 1
    While I believe this is off topic, check out [Fluent Validation](https://www.nuget.org/packages/FluentValidation/). – Greg Burghardt Feb 09 '17 at 18:33
  • Great @GregBurghardt! Didn't know about this at all. Although - I don't believe it helps me point to a particular method/action/validation implicitly to validate `bar` based on `foo`'s value. Thanks again. – JᴀʏMᴇᴇ Feb 09 '17 at 23:30
  • It can do exactly that. It just won't be with AJAX. You'll have to POST the whole form back and redraw the page with validation errors. – Greg Burghardt Feb 10 '17 at 12:37
  • @GregBurghardt - thanks again, mate. Do you have an example of how you could use fluent validation to achieve this? – JᴀʏMᴇᴇ Feb 10 '17 at 14:35

0 Answers0