Validate With Method not attaching error to optional field
Reported by ru_ghetto (at rubyghetto) | August 2nd, 2009 @ 01:54 PM
Given a model with the validation below, a produced error will be attached to the hash key :validate_username_unique instead of to :username.
validates_with_method :username, :validate_username_unique
def validate_username_unique
if new_record? || changed?(:username)
if self.class.by_username( :key => self.username, :limit => 1).blank?
return true
else
return [false, "Username has already been taken"]
end
else
return true
end
end
# note: changed?(field) is a method added to the class, that is tested and works
Here is what the error inspection looks like:
<CouchRest::Validation::ValidationErrors:0x23a2e18 @errors={:validate_username_unique=>["Username has already been taken"]}>
No comments found
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
"CouchDB, close to the metal." <a href="http://github.com/jchris/couchrest/tree/master">CouchRest</a> is a RESTful layer for accessing CouchDB, based off CouchDB's included Javascript reference client. CouchRest also includes helpers for running large queries etc. There is also a base class for ActiveRecord / Datamapper style ORM, called CouchRest::Model.