I’m curious about this:
In Microsofts Outlook Express (or Outlook, don’t remember well, I’m a Mac user), they have something really cool. Generic rules:
You can configure a set of rules to automatically sort or delete your emails, for instance. It’s incredible powerful and easy to use.
These rules looked pretty much like this:
“If email in inbox has subject which contains ‘foo’, or ‘bar’, or ‘foobar’ delete it”
I need to code something similar for a powerful form validation system.
The developer should simply be able to create rules like this:
rule: [password_1] is_not_equal_with [password_2]
consequence: show_error '2921'
rule: [firstName] has_less_characters_than '2'
consequence: show_error '1211'
rule: [age] is_numeric, is_smaller_than '13', is_greater_than '130'
consequence: show_error '1522'
rule: [gender] is_equal_with 'female'
consequence: show_group [female_questions]
rule: [termsAndConditionsAccepted] is_not_checked
consequence: show_error '482'
rule: [age] is_less_than 21
consequence: hide_group [income_questions]
Well, I have some ideas how this could be done, and I will post them here as answer. But before I reinvent the wheel: Are there any written concepts I can use as foundation to develop a rule-based validation system similar to this? Or if not, do you have any suggestions how this could be done?
In the example above, everything in square brackets is the name of an html form element. Everything in apostrophs ” is a “hard coded” value to compare against.
The defined rules are translated into PHP code AND JavaScript code to do both client- and server side validation.
Features this must be capable of:
- Conditional rules: Something A depends on something B
- Value comparisons: For integers, floats, strings
- Enable some form control logic as well, like in the “[gender] is_equal_with ‘female'” example above.
How could this be done? What are the entities I must consider, from a scientific point of view?
I think the theoretical concept of this is platform independent. Although I will implement this in PHP and JavaScript, there’s no reason why a C++ dev should not respond 😉
(I’m an Objective-C guy, btw)
You might want to check out some of the opensource rules engines; or even a paid for one.
Examples include
Pay for it:
InRule, Business Rules Engine, ASA Business Rules Engine
Opensource:
OpenRules, Drools
There are a lot more. Including some built in to java (Java Rule Engine API (JSR94)), and .Net (Windows Workflow Foundation Rules Engine).
Not sure about straight PHP though.
As a side note, I’ve used a couple engines, like Haley Rules (before they were bought by Oracle) to drive web UI’s. Be aware that execution speed is absolutely critical. We had Haley processing about 2000 rules per page load (mortgage app), and it was executing in under 40 ms (not a typo). We used it to decide what fields were on the page as well as determine whether the entered data was consistent, met legal standards, and even whether it was entered correctly.
Some of the other engines were much much slower even on much smaller rule sets due to how long it took to simply instantiate the engines.
I’ve also gone down the path of writing my own for smaller systems. In my case I used javascript and simply set up variables with data from the posted page prior to executing the scripts that were saved with the forms.
This was also performant on a smaller scale, but I limited it to only giving simple go / no go responses.