开发者

HTML form requirements specification

I am building a framework that will validate forms both c开发者_JAVA百科lient-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.


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.


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.


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜