简体   繁体   中英

Backbone: Partial client side validation + server side validation

I have been playing around with backbone for a while now. I really like the backbone.validation library but it only does simple client side validations.

In some of my projects I have validations that can only be run on the server (Rails). In my ideal world, I would be able to have the server return a 422 (Unprocessable Entity) and provide me with a mapping of the attribute(s) and their corresponding errors so that I can highlight them in the UI.

Has anyone run into this problem?

It is good to have server-side validation.

However if your client-side validation is thorough enough and can do the highlighting you want then you shouldn't need to return full highlighting parameters to the browser, you can just prevent the submission roundtrip and do whatever UX you want on the client side.

I've used the jQuery valitation plugin to this end with great success, full error messaging and highlighting are possible and all the validation that is necessary in one plugin. Not sure how it compares to the Backbone validation.

http://jqueryvalidation.org/

While client side validation is great for user experience, server based applications should never rely on client side validation alone as it can easily be bypassed and allow attacks. If you choose to have client side validation for UX purposes great, but you should always validate server side. See here or here for more info, or just google "issue with client side only validation".

That said, our backbone + rails application does indeed return the 422 (Unprocessable Entity) error when a validation fails on the rails model. If you do not see this then you are either not validating on your rails model object, or you have some sort of disconnect between rails and backbone models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM