Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make sure the validations are executed initially

  • Loading branch information...
commit d18641e270593a9945e1cd59e332c2bfefb5882c 1 parent ee5380a
@dnagir authored
View
3  lib/assets/javascripts/knockout/validations.js.coffee
@@ -42,6 +42,9 @@ class ValidationContext
me.subject.errors[field](null)
, me.subject, "beforeChange" if me._validations[field].isEmpty()
+ # Enforce validation right after enabling it
+ validatorSubscriber.notifySubscribers( validatorSubscriber(), 'change')
+
me._validations[field].push validatorSubscriber
me
View
24 spec/javascripts/knockout/validations_spec.js.coffee
@@ -20,6 +20,9 @@ describe "Validations", ->
beforeEach ->
@subject = new Page()
+ it "should initially be invalid", ->
+ expect(@subject.errors.name()).toBeTruthy()
+
it "should set error on a field", ->
@subject.name ''
expect(@subject.errors.name()).toMatch /blank/i
@@ -41,6 +44,27 @@ describe "Validations", ->
expect(@subject.errors.multiple()).toBe "xxx, xxx, xxx"
+ describe "when re-setting JSON", ->
+
+ it "should use existing observer", ->
+ prevName = @subject.name
+ @subject.set name: 'abcdef'
+ expect(@subject.name).toBe prevName
+
+ it "should change the values", ->
+ @subject.set name: 'abcdef'
+ expect(@subject.name()).toBe 'abcdef'
+
+ it "should make field valid", ->
+ @subject.set name:'a'
+ expect( @subject.errors.name() ).toBeFalsy()
+
+ it "should make field invalid", ->
+ @subject.name 'valid'
+ @subject.set name:''
+ expect( @subject.errors.name() ).toBeTruthy()
+
+
describe "conditional validations", ->
Please sign in to comment.
Something went wrong with that request. Please try again.