Skip to content

Instantly share code, notes, and snippets.

@aiwilliams
Created June 22, 2011 20:59
Show Gist options
  • Save aiwilliams/1041179 to your computer and use it in GitHub Desktop.
Save aiwilliams/1041179 to your computer and use it in GitHub Desktop.
A glimpse at how I'm doing validations.
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)
@aiwilliams
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment