6

I'm working on an MVC page that requires conditional validation.

When a user selects a country from a dropdownlist, if they select one of two specific countries, then a box is displayed containing two text boxes which are required. I would like validation to activate in this case, and if they select any other country, then the box is hidden and validation will be deactivated.

Currently on the site, which I didn't build, there is a separate validation class (which inherits from ValidationSet) that handles all validation for that controller, and they validate with commands like ValidatePresence, ValidateDecimal, and ValidateExpression, so I would like to stick to that format for consistency. e.g.

new ValidatePresence("countryId") {ErrorMessageFormat = "Please supply a country for delivery to"}

Anyone got any ideas? Thanks

e-on
  • 1,547
  • 8
  • 32
  • 65

2 Answers2

3

Is there anything wrong w/ just having another validator like, "ValidateConditionalPresence" or the like, then having it do what you said? i.e.

  return dropdown == false || (!string.IsNullOrWhitespace(box1) && !string.IsNullOrWhitespace(box2));
Paul
  • 35,689
  • 11
  • 93
  • 122
1

My apologies here - I initially though ValidatePresence, ValidateDecimal etc were validation controls within MVC, but discovered that they are custom classes. I have now amended these to do what was required.

Thanks for your time anyway

e-on
  • 1,547
  • 8
  • 32
  • 65