Run validations early on

I just had the case in a Rails app where the user’s email doesn’t have to be valid. The validations in this case are done by Devise. If the email isn’t valid I want to remove it. The way to do this is to run all validators on email before the actual validation and then decide wether to set email to nil.

1
2
3
4
before_validation do
  User.validators_on(:email).map{|v| v.validate(self) }
  self.email = nil if errors[:email]
end