views:

93

answers:

3

I am building a framework that will validate forms both client-side (javascript) and server-side based on a form requirements specification written in json.

The purpose is to get rid of logically equivalent code on the server and client to make the code more maintainable, faster to write, and less buggy.

The specification format may look something like:

{ '<field_name>' : ['<validation_function>', 'req', ['<requirement>', <param>], ...], ... }

( the requirement list is ordered so that the user can get most basic error messages first, the 'req' requirement must come first if it exists and means that the field is required)

e.g.)

{
    'name' : ['string', 'req', ['min',6], ['max',150], ['match', /^[\sa-z0-9ÅÄÖåäö&]$/i], ['not_match', /^tmp_/]],
    'email' : ['email', 'req'],
    'email_confirm' : ['same_as', 'email'],
    'password' : ['string', 'req', ['min', 6], ['max', 64], ['match', /^[a-z0-9\!@#\$%^&*_+.]$/i] ],
}

Does anyone know of a similar technology? I think the Rails validation framework solves the problem on the wrong level because I have found that forms often operate on more than one model.

A: 

I know Django has a validation framework too. But it happens in the back-end as well.

But what if the back-end is the only place that can know whether the entered data is valid? Maybe the user of this validation system wants to make sure the e-mail address isn't already being used for another account, for instance. Then that check cannot happen client-side.

mojbro
Yes some checks can only be done at the server side. But, I am only trying to solve the problem with validations that should be done both client-side and server-side. There is no duplication of code for server-side-only checks.
Peder
A very good idea. I cannot give you a good answer on your question other than to say your idea is good!
mojbro
A: 

Have you considered reusing the backend validation via XMLHttpRequest and returning any error details as JSON suitable for easily updating the form to show errors? Simon Willison put an example of doing so using Django's excellent django.forms module in his Advanced Django presentation (from slide 56).

Nice as this is, I'd also like to scratch the same itch which drives your question, which is why I started porting django.forms to JavaScript in js-forms, which is pretty far along, but also far from ready for its intended use, which is using the same core codebase to run appropriate validations on the frontend and backend using the same Form object definitions.

insin
Sending ajax validation requests to the server is slow and wasteful for most validation purposes. Thanks for the link to your project and the django.forms api. It looks interesting.
Peder
A: 

But but... there's been ways to do nested models/forms in rails before, and in v2.3 it got some nice support, didn't it?

See rails 2.3 releasenotes (#nested object forms))

Not that everybody uses rails, of course :)

npup
The nested object forms improvement looks interesting. Thanks for that link. However I still don't know how to create requirements for multi-page forms using Rails validations framework. And how can I get Rails validation framework to generate jquery compatible javascript validation?
Peder