开发者

Rails Validation for users email - only want it to validate when a user signs up or updates email address

开发者 https://www.devze.com 2022-12-10 00:59 出处:网络
I have a User model with the usual attributes such as email and hashed_password etc. I want to write a validation that checks for the presence of an email address but only when

I have a User model with the usual attributes such as email and hashed_password etc. I want to write a validation that checks for the presence of an email address but only when

1) there isn't one stored in the database for this object (i.e. this is a new user signing up)

2) the user is trying to update their ema开发者_开发百科il address.

My current validations

  validates_presence_of :email
  validates_presence_of :email_confirmation

  validates_confirmation_of :email

are obviously preventing me from updating any attributes. I thought of using

validates_presence_of :email , :if :email_validation_required?

def email_validation_required?
self.email.blank?
end

But that wont help with scenario 2 as it will return true because the user does have an password email address in the db.

I cant work out how i can limit it to just those 2 scenarios above.

Can anyone help?


I think EmFi is on to something. But I don't think the validates_presence_of :email should be holding you up. The email should always be present - if it is left blank in the form the parameter will not mess with your save of the user. If it is entered in the form, even for update, it should have an email_confirmation along for the ride.

Give this a try:

validates_presence_of :email
validates_presence_of :email_confirmation, :if => :email_changed?

validates_confirmation_of :email


you have two options. one is to use :on. by default, they are set to :on => :save, but you can do something like this this

validates_presence_of :email, :on => :create

or

validates_presence_of :email, :on => :update

the other options is to use :if, and then pass in a method name or a proc. so something like

validates_presence_of :email, :if => :should_validate

or

validates_presence_of :email, :if => Proc.new { |user| user.signup_stage > 2 }

Hope that helps :)


You want to use an :if clause on the validation that uses the ActiveRecord::Dirty methods:

validates_presence_of :email, :if => Proc.new { |user| user.email_changed?}

N.B. Only works in Rails 2.1 or later.

0

精彩评论

暂无评论...
验证码 换一张
取 消