Validation question

hello everyone,

i have a question about how validates_presence_of works as it pertains
to a login system. when you create a user for the first time you can
require that both their password and confirmation password are present.
but on subsequent updates to the that record you have to enter a
confirmation password and that’s really annoying.

i’ve been looking around and i’ve seen solutions that like something
like tihs:

validates_presence_of :confirmation_password, :if=>‘required’

where does that “:if” part come from? i mean, how did whoever suggest
that know that they could do that? is that a Rails or a Ruby thing?

any insight into that mechanism would be much appreciated.

thanks in advance,

binh

where does that “:if” part come from? i mean, how did whoever suggest
that know that they could do that? is that a Rails or a Ruby thing?

any insight into that mechanism would be much appreciated.

It’s a Rails thing…

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000813

Configuration options:

  • message - A custom error message (default is: “can.t be blank”)
  • on - Specifies when this validation is active (default is :save,
    other options :create, :update)
  • if - Specifies a method, proc or string to call to determine if the
    validation should occur (e.g. :if => :allow_validation, or :if =>
    Proc.new { |user| user.signup_step > 2 }). The method, proc or string
    should return or evaluate to a true or false value.