I'm working on a Pylons-based web app. Because I am sane, I am using jQuery (and plugins) instead of writing raw JavaScript. I am also using FormEncode to validate forms for my app (especially new user registration). FormEncode is great for validating forms after they're submitted. jQuery, when JavaScript is available, validates forms quite well before they're submitted.
I'm greedy: I want to use both kinds of validation - and I don't want to repeat myself. If there are two sets of validation rules, there's an extra workload generated by keeping them in sync.
How can I use jQuery to a开发者_运维技巧ccess my FormEncode validation rules, so that both jQuery and FormEncode investigate form data based on the same rules without having to write the rules down twice ?
My current solution is to have the FormEncode rules in the controller, and to give the controller one method that responds to a complete form being submitted, and another method that responds to AJAX requests, and validate both methods again the same FormEncode rules. That means that I can have jQuery make requests to the controller's AJAX-y method, and have regular form submissions go to the controller's normal form-submission method, and in both cases the same set of form-validation rules will be applied.
精彩评论