Created
June 22, 2011 20:59
-
-
Save aiwilliams/1041179 to your computer and use it in GitHub Desktop.
A glimpse at how I'm doing validations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define ['backbone', 'validate/validators'], (Backbone, Validators) -> | |
Validators.extend Backbone.Model | |
Child: class Child extends Backbone.Model | |
defaults: | |
name:'' | |
email:'' | |
invited:false | |
@required 'name' | |
@required 'email', when:'invited' | |
Coparent: class Coparent extends Backbone.Model | |
defaults: | |
name:'' | |
email:'' | |
invited:false | |
timezone:'' | |
@required 'name', 'timezone' | |
@required 'email', when:'invited' | |
Cofamily: class Cofamily extends Backbone.Model | |
initialize: -> | |
@children = new Backbone.Collection | |
@primaryCoparent = new Coparent | |
@coparent = new Coparent | |
addChild: (attrs) -> | |
@children.add new Child(attrs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The validators are declared in the models, but all the work is done with a ModelFormValidator, which knows how to use the validators to validate the values of visible HTML form fields using Validator#validate(model, value). Validator knows the attribute it's responsible for and has options like when:.