Permalink
Browse files

validation no longer piles up error messages, also only validates att…

…ributes passed into set. and bump version.
  • Loading branch information...
Chris Nelson
Chris Nelson committed Jul 17, 2012
1 parent afbbc2b commit 9fe9c256c2e7d8aee87189cddcea1a6e257f0383
@@ -1,5 +1,5 @@
class Person < ActiveRecord::Base
- # validates_presence_of :first_name
+ validates_presence_of :first_name
belongs_to :occupation
end
@@ -0,0 +1 @@
+Jasminerice::Runner.capybara_driver = :webkit
@@ -9,12 +9,17 @@ describe "Person", ->
describe "failing validation", ->
beforeEach ->
@person.set first_name: "", last_name: "NotJones"
- it "should not be valid", ->
- expect(@person.isValid()).toBeFalsy()
it "should have triggered the error event for first name", ->
expect(@validationErrors.first_name.length).toEqual 1
it "should have triggered the error event for last name", ->
expect(@validationErrors.last_name.length).toEqual 1
+ describe "with only one attribute", ->
+ beforeEach ->
+ @person.set last_name: "Jones"
+ it "should only validate attributes that being set", ->
+ expect(@person.isValid()).toBeTruthy()
+ it "should have set the last name", ->
+ expect(@person.get("last_name")).toEqual "Jones"
describe "with valid attributes", ->
beforeEach ->
@person.set first_name: "Fred", last_name: "Jones"
@@ -11,7 +11,24 @@ describe "text field view", ->
parentView: new Example.Views.FormView
@textFieldView.render()
it "renders has a value", ->
- expect(@textFieldView.$("input[name=name]").val()).toEqual "bob"
+ expect(@textFieldView.$("input[name=name]").val()).toEqual "bob"
it "puts a label on it", ->
expect(@textFieldView.$("label[for=name]")).toExist()
expect(@textFieldView.$("label[for=name]")).toHaveText /Name/
+
+ describe "displaying errors", ->
+ beforeEach ->
+ @textFieldView.displayErrors
+ name: ["totally borked"]
+ it "should add the error class", ->
+ expect(@textFieldView.$el).toHaveClass "error"
+ it "should add the error message", ->
+ expect(@textFieldView.$("span.help-inline")).toHaveText "totally borked"
+ describe "again", ->
+ beforeEach ->
+ @textFieldView.displayErrors
+ name: ["totally borked"]
+ it "should only display it once", ->
+ expect(@textFieldView.$("span.help-inline").size()).toEqual 1
+
+
@@ -27,13 +27,16 @@ Backtastic.Validation =
clearErrors: -> @errors = {}
+ validateAttribute: (attribute, value) ->
+ return unless @constructor.validations[attribute]
+ for validator in @constructor.validations[attribute]
+ error = validator(attribute, value)
+ @addError(attribute, error) if error
+
validate: (attributes) ->
@clearErrors()
return unless @constructor.validations
- for attr, validators of @constructor.validations
- for validator in validators
- error = validator(attr, attributes[attr])
- @addError(attr, error) if error
+ @validateAttribute(attr, value) for attr, value of attributes
@errors if _.keys(@errors).length > 0
Backtastic.include Backbone.Model, Backtastic.Validation
@@ -6,7 +6,7 @@ class Backtastic.Views.FormFieldView extends Backtastic.View
@label = options.label
@parentView = options.parentView
@parentView.on "rendered", => @afterParentRender()
- @model.on "error", (model, errors) => @displayErrors(errors)
+ @model.on "error", (model, errors) => @displayErrors(errors)
afterParentRender: ->
@setElement(@parentView.$("[data-view-id=#{@cid}]"))
@@ -21,6 +21,7 @@ class Backtastic.Views.FormFieldView extends Backtastic.View
if errors?[@field]
messages = errors[@field]
@$el.addClass "error"
+ @$("span.help-inline").remove()
@$el.append "<span class='help-inline'>#{message}</span>" for message in messages
clearErrors: ->
@@ -1,3 +1,3 @@
module Backtastic
- VERSION = "0.0.2"
+ VERSION = "0.0.3"
end
View
@@ -0,0 +1 @@
+require 'backtastic'

0 comments on commit 9fe9c25

Please sign in to comment.