27

Since we're using Backbone we're not really submitting forms to a server every time we pop up a UI for the user to enter some data. But several of the popular validation frameworks assume you have a <form> to go with your various inputs (and we don't) and that you're going to submit that form (I'm not).

What validation framework pairs well with something and allows me to trigger when it validates and doesn't care whether my controls are in a form or not?

Nickolay
  • 31,095
  • 13
  • 107
  • 185
John Munsch
  • 19,530
  • 8
  • 42
  • 72
  • What validation frameworks have you considered? – erturne Oct 23 '11 at 06:05
  • The jQuery plugin for validation (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) was the main one. After I had used that quite a bit on my last project I became disillusioned about using another one so closely tied to forms. Most recently I looked at validate.js (http://rickharrison.github.com/validate.js/) and realized it made the same basic assumptions (form + submission = validation). That's why I thought I should ask around to find a better solution. – John Munsch Oct 24 '11 at 13:33

4 Answers4

3

Interesting question. I haven't had luck finding a 100% solution myself so don't see this as a direct answer to your question, but it might help how this could potentially be tackled best IMO.

To me, the best fit would be validation-rules completely defined in Json, with both a client-side and server side declarative validation-'builder' that would build the rules from the definition. This would enable DRY (server / client) validation.

JSON-schema is pretty limited for this.

I believe Kansojs.org has a pretty good JSON definition for validation, but I have yet to come across the builders that implement it.

I raised a question about this some time ago, but I fear my essay-like question triggered no response :) DRY user-input validation (clientside, serverside) using JSON-schema

hope that helps some, Geert-Jan

Community
  • 1
  • 1
Geert-Jan
  • 18,623
  • 16
  • 75
  • 137
1

I was looking for a cross-platform (nodejs, browser) validation lib for backbone, which supports asynchronous tests, custom tests, custom language error messages, test relations, etc...

I haven't found a standalone lib for that, but I found a solution here: https://github.com/powmedia/backbone-forms , it generates the whole form instead of just validating it, and has custom error messages too...

Generating the forms with the validator is a huge idea, btw. I think Backbone-forms is not flexible enough and it is too big for me. I want to use Backbone.UI, and it has a custom data binder. I don't know how to customize Backbone-forms to use the Backbone.UI data binder.

I created my own lib: https://github.com/inf3rno/bb-validation , it contained only validation in version 1.0, now I refactored and rewrote parts of the code, and added a form generator to version 2.0. That form generator uses http://perka.github.io/backbone-ui fields, and my goal is to develop something that is similar to backbone-forms but consists of smaller libraries. I think this is the way to write reusable javascript code...

inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • Thanks for adding a new answer and that's an excellent note about the restriction on error messages. That's not something I would have necessarily thought about (and which, strangely, my current employer does not care about) but many websites have to keep in mind. – John Munsch Oct 31 '12 at 00:27
  • Yepp I'll write a hungarian application, so the english property names in the error messages is not an option. :S I have to write my own validator I guess... – inf3rno Oct 31 '12 at 00:41
1

We've recently come across this library: https://github.com/thedersen/backbone.validation

At least on paper, it looks great. It doesn't require forms instead seemingly quite happy to validate at the model level. It also has some simple ability to update elements within the view to reflect errors in validation if there is a simple mapping between element and attribute on the model.

Once we've had a chance to exercise it thoroughly I'll come back and update this with more information.

John Munsch
  • 19,530
  • 8
  • 42
  • 72
  • How are you liking this? I'm having a hell of a time trying to extend it set a value to a default if you try to set an attribute to an invalid value. – tkone Feb 22 '12 at 20:03
  • I ended up not doing much validation yet in the code I've worked on (it was a bunch of complicated display of data and user interaction with it, but no real data entry). I'm told by a co-worker that we are using a rewritten version of the validation we started with because we weren't crazy about how it worked in practice. So I think what we've got at the moment is fairly homegrown. – John Munsch Feb 23 '12 at 02:15
  • That's exacty the route we started going down. We wanted the validation framework to use the default value, if any, should the new value not validate. The two big ones out there don't have that option so we started hacking away. Hopefully we'll convince our owners to open source it all! – tkone Feb 23 '12 at 05:39
0

Why note use and override the original validation entry point of Backbone? Here is the link to the documentation:

http://backbonejs.org/#Model-validate

And from the documentation:

validatemodel.validate(attributes, options) 

This method is left undefined and you're encouraged to override it with any custom validation logic you have that can be performed in JavaScript.

By default save checks validate before setting any attributes but you may also tell set to validate the new attributes by passing {validate: true} as an option.

The validate method receives the model attributes as well as any options passed to set or save. If the attributes are valid, don't return anything from validate; if they are invalid return an error of your choosing.

It can be as simple as a string error message to be displayed, or a complete error object that describes the error programmatically. If validate returns an error, save will not continue, and the model attributes will not be modified on the server. Failed validations trigger an "invalid" event, and set the validationError property on the model with the value returned by this method.

-- Also use and override this two functions:

validationErrormodel.validationError 

The value returned by validate during the last failed validation.

isValidmodel.isValid() 

Run validate to check the model state.