admin管理员组

文章数量:1279051

Backbone.js es with model validation events, and I'm looking to integrate it with a lite validation framework.

I just started looking at /

any other remendations?

Backbone.js es with model validation events, and I'm looking to integrate it with a lite validation framework.

I just started looking at http://happyjs./

any other remendations?

Share Improve this question asked May 31, 2011 at 18:23 Edward ChanEdward Chan 8031 gold badge10 silver badges7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

jQuery Validate is my favorite its well documented and used by many

Try the Backbone.Validations plugin

There are some validations plugins for backbone but I don't like it, I ended up with this:

...
validate: (attrs) ->
  helper = @validationHelper attrs
  helper.check 'name', (v) -> "can't be blank" if _(v).isBlank()
  helper.check 'version', (v) -> "can't be blank" if _(v).isBlank()
  helper.check 'organization', (v) -> "can't be blank" if _(v).isBlank()
  helper.errors()
...

And here's the helper:

  _(Backbone.Model.prototype).extend
    validationHelper: (attrs) ->
      helper = 
        _errors: {}
        check: (name, validator) ->
          return unless attrs and (name of attrs)
          msg = validator attrs[name]
          (@_errors[name] ?= []).push msg if msg
          @
        errors: -> if _(@_errors).any() then @_errors else null
      return helper

本文标签: Javascript Validation Framework for BackbonejsStack Overflow