Reputation: 7910
I have a model, let's call it Book
. In rails, when the Book is saved, I validate the uniqueness of it's ISBN number. For my front end, I have a simple SpineJS app which let's me add a new book.
In SpineJS:
class App.Book extends Spine.Model
@configure 'Book', 'name', 'isbn'
@extend Spine.Model.Ajax
validate: ->
"Name required" unless @name
"ISBN required" unless @isbn
And in Rails:
class Book < ActiveRecord::Base
attr_accessible :name, :isbn
validates :name, :presence => true
validates :isbn. :presence => true, :uniqueness => true
end
My issue is that in my SpineJS app, it happily saves a new book with a duplicate ISBN number, even though the Rails server returns validation errors.
Is there some way to handle this error client-side on save?
Upvotes: 1
Views: 180
Reputation: 4492
Spine's manual claims:
If validation does fail server-side, it's an error in your client-side validation logic rather than with user input.
I don't see how this could easily work with your uniqueness requirement. It may be workable if you can load all database data that could affect validation into the client side and you can somehow avoid all multi-user race conditions.
You can catch the "ajaxError" event and tell the user to retry, although catching "ajaxError" goes against the recommendations in the manual. IIRC you may also have to do some juggling with object IDs to convince Spine that a new record was in fact not created.
Additionally, you can fire pre-emptive validation requests as the user is editing the data, but this is just for the user's convenience. In theory you can still hit a race condition where someone else creates a conflicting record just before the user hits save.
Personally I switched to Backbone since I found Spine's careless attitude to error handling too scary.
Upvotes: 1