For my framework I've written down this concept to solve the form validation problem. I want it to be as straightforward as possible for the framework user.
Every Form (=object) has one or many FormElements (=objects). Every FormElement can have 0-n FormValidators (=objects). Everything easily configured through the backend (simple drag&drop stuff).
When the Form View is rendered, it loops over all the FormElements, and for each of them it loops over all their associated FormValidators. That way it builds up all the needed JavaScript to validate the form on client side.
A FormValidator is an lightweight object which defines only these seven things:
- PHP class name of the validation utility class
- method name of the validation utility class, which must be called
a string for additional arguments (comma-separated values)
JavaScript "class" name of the validation utility
- "method" name which must be called
a tring for additional arguments (comma-separated values)
an associated ErrorInfo object which contains an formatted error message
Every of these validation methods takes as first argument an input variable with the input data. Every of these methods just check the input if it matches some rule(s), and returns TRUE or FALSE.
When the form is submitted, an FormDataManager is created and receives: - the Form object (so it knows from where the data came from) - the input data (typically $_POST)
It then just iterates over all the FormElements, checks all their FormValidators, and if anything is invalid, it reloads the Form which is passed the error messages array. If everything is fine, the data is further processed (i.e. stored to db).
Are there improvements in this design? Anything I've missed?