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.

  • 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 04 '13 at 06:14
  • 3
    Start reading this http://www.codeproject.com/Articles/241066/How-to-Validate-ASP-NET-Web-Forms-Using-Business-R, the rest will become obvious :) – Kizz May 06 '13 at 01: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 09 '13 at 09:39
  • @LucFranken, Yes, cuz A and B are selected, D is invalid. That is the business rule -- in this case. – blindcodifier9734 Jun 24 '13 at 00: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. – blindcodifier9734 Jul 06 '13 at 20:21

3 Answers3

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.

Vlad GURDIGA
  • 111
  • 4
  • 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. –  Jun 09 '13 at 03: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.

mouviciel
  • 15,473
  • 1
  • 37
  • 64
  • 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. – blindcodifier9734 Jul 06 '13 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.

Luc Franken
  • 2,664
  • 15
  • 9
  • 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. – blindcodifier9734 Jul 06 '13 at 20:25