alexdunae/validates_email_format_of — Issue #25: Validation with Rails 3.x are not cleaning up model.errors correctly
Author: brodock
Created: 2011-11-16 03:16:06+00:00
State: open

I haven't tried Rails 2.x yet as I'm not using it anymore, but with Rails 3 (3.1 more specifically), If I issue a model.valid? and got e-mail validation issues, if I went there and fix it, while i issue another model.valid? if only cleans up the "value" of the hash, but the key ":email" stills there.

I got something like that:

model.email = 'wrong@invalid'
model.valid?
=> false
model.email = 'validmail@gmail.com'
model.valid?
=> true
model.errors
=> #, @messages={:email=>[]}>

It's not causing any harm yet, but it should not add error key at all as there is no error found.

I've came up with a fix on Unit Tests that should catch this kind of error.

Your 'save_passes' method should be modified to:

def save_passes(p, email = '')
assert p.valid?, " #{email} should pass"
assert p.save
if ActiveRecord::VERSION::MAJOR >= 3
assert p.errors[:email].empty? && !p.errors.key?(:email) # this catches p.errors with key defined, but without messages
else
assert_nil p.errors.on(:email)
end
end