4

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

<select id="options" multiple="multiple">  
<option>A</option>  
<option>B</option>  
<option>C</option>  
<option>D</option>  
<option>E</option>  
</select>  

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.

5
  • 2
    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, 2013 at 6:14
  • 3
    Start reading this codeproject.com/Articles/241066/…, the rest will become obvious :)
    – Kizz
    May 6, 2013 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. Jun 9, 2013 at 9:39
  • @LucFranken, Yes, cuz A and B are selected, D is invalid. That is the business rule -- in this case. Jun 24, 2013 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. Jul 6, 2013 at 20:21

3 Answers 3

1

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.

1
  • 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, 2013 at 3:31
1

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.

1
  • 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. Jul 6, 2013 at 19:06
0

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

Example: https://stackoverflow.com/questions/14217442/cakephp-validation-depending-on-other-field

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.

1
  • 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. Jul 6, 2013 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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