Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to validate some form inputs in complex way. E.g.

<select id="options" multiple="multiple">  

For example, if user selects A and B, user should be given an error if user also selects D.

Note, I'm trying to keep this business rule in a separate layer from this UI code, which could be an entirely different form, e.g. checkboxes, drag-and-drop, add/delete options with possibly accompanying fields.

I'm looking for a framework, design pattern, or other elegant way to code this logic, preferably ultimately through validation attributes in a model.

share|improve this question
The logic you describe is not complex. Please specify the technology you are using so that you could get a precise answer. As a rule always validate on the server, if you are using classes, you could add the validation as a method in the class this would be one way of making the logic shared. You could (and in most cases should) validate on the server, most probably using JavaScript (which won't work if your user shuts down JavaScript). – NoChance May 4 '13 at 6:14
Start reading this…, the rest will become obvious :) – Kizz May 6 '13 at 1:00
Can you explain your business rules better? Why is D invalid? Cause A + B are selected? Cause A can go with max. 1 other option? You need more details to get the best answer possible. – Luc Franken Jun 9 '13 at 9:39
@LucFranken, Yes, cuz A and B are selected, D is invalid. That is the business rule -- in this case. – awgtek Jun 24 '13 at 0:06
I think I would have accepted Kizz' solution as an answer because it has a DSL and is therefore more robust. Too bad it's not free though. – awgtek Jul 6 '13 at 20:21

Approaching the question from a different angle: Why not disable D if the user selects A and B? When I have this kind of question I start to think about reviewing my UI flow.

share|improve this answer
If there is a way to subvert the ui (e.g. turn off JavaScript), then one cannot trust the client. This should still be considered if there is a client that is stand alone (native app) and passing messages to the server in an uncontrolled environment. – user40980 Jun 9 '13 at 3:31

For representing the validation logic of your inputs, you can use a Karnaugh map, in which inputs are the selection state of A, B, C, D and E, and the outcome is the validation status of each input combination.

  AB-CDE | 000 | 001 | 011 | 010 | 110 | 111 | 101 | 100 |
  00     |     |     |     |     |     |     |     |     |
  01     |     |     |     |     |     |     |     |     |
  11     |     |     |  0  |  0  |  0  |  0  |     |     | -> Error if (A.B).D
  10     |     |     |     |     |     |     |     |     |

From that map, you can find the minimal logic expression validating your check boxes.

share|improve this answer
This is an interesting idea. The problem is I need an example of how to use this in code. And more specifically handled by some open-source framework -- to be sure I'm not reinventing the wheel by writing spaghetti code. – awgtek Jul 6 '13 at 19:06

Most frameworks can validate at the model level. So that should be sufficient. So you validate at the lowest possible level.


If you want to show this in the interface (with ajax for example) you just call the model to validate (via the controller probably). Create a method in the controller like: validateOptions() and send an ajax call to it so you can modify the user interface.

When really saving the validation will be done again to ensure it's right.

share|improve this answer
This is a good solution and similar the jQuery validation component as well as that of other frameworks. However, I guess I'm looking for something more abstract, e.g. a pluggable DSL like Kizz suggestion. – awgtek Jul 6 '13 at 20:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.