Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed validation does not trigger error callback #2153

Closed
mbuchetics opened this issue Jan 17, 2013 · 9 comments
Closed

Failed validation does not trigger error callback #2153

mbuchetics opened this issue Jan 17, 2013 · 9 comments
Labels

Comments

@mbuchetics
Copy link

Version 0.9.10 does not trigger the error callback when validation fails on a save. This used to work until 0.9.9. The following line was removed in 0.9.10:

if (options && options.error) options.error(this, error, options);

The comment above the method reads

// Run validation against the next complete set of model attributes,
// returning `true` if all is well. Otherwise, fire an
// `"invalid"` event and call the invalid callback, if specified.

An "invalid" event is triggered, but no callback is called. Furthermore, the documentation doesn't mention an "invalid" event or callback, it's still called "error" there:

"error" events are useful for providing coarse-grained error messages at the model or collection level. An error callback can can also be specified in the options, to be called alongside the error event:

@Derbeth
Copy link

Derbeth commented Jan 17, 2013

Running code example in #2154. Sorry for duplicating, but you were only 30 minutes faster than me ;-)

@caseywebdev
Copy link
Collaborator

This hasn't been documented very well yet, but due to the ambiguity of a catchall error callback for validate and xhr errors, validate is being handled differently. If there was an error returned by your validate function, the returned value will be stored in model.validateError.

model.save({bad: 'attr'}, {
  success: function () {}, // all good
  error: function () {} // xhr error
})
if (model.validationError) {
  // validate error(s) accessible in model.validationError
}

This pattern was chosen rather than a callback because validate will always be synchronous, as opposed to success and error.

@mbuchetics
Copy link
Author

Ok, makes sense, thanks. Please update the documentation for the 1.0 release.

@tgriesser
Copy link
Collaborator

Keeping this one open until the docs get updated, just so we don't forget.

@tgriesser tgriesser reopened this Jan 17, 2013
@joezimjs
Copy link

Also, the docs haven't been updated to say that 'invalid' is the new event name instead of 'error'.

@tgriesser
Copy link
Collaborator

Everything should be documented on this one now, closing.

@ghost
Copy link

ghost commented Jan 22, 2013

The documentation for Model validate (http://backbonejs.org/#Model-validate) still mentions being able to pass in an error callback on the set method.

@Derbeth
Copy link

Derbeth commented Jan 23, 2013

@caseywebdev : model.validationError you give in your example is not mentioned anywhere on the Backbone site.

@braddunbar
Copy link
Collaborator

@Derbeth That documentation was added in #2172 and will be public with the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants