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?
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?
Share Improve this question edited Oct 23, 2011 at 0:34 Nickolay 32.1k13 gold badges110 silver badges194 bronze badges asked Oct 22, 2011 at 22:48 John MunschJohn Munsch 19.5k8 gold badges44 silver badges74 bronze badges 2- What validation frameworks have you considered? – Eric Turner Commented Oct 23, 2011 at 6:05
- The jQuery plugin for validation (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 (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 Commented Oct 24, 2011 at 13:33
4 Answers
Reset to default 3Interesting 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
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.
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...
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.