How can I selectively turn of validations

Hi,

   I need to selectively turn off validations for example, I want to make sure that password and password_confirmation fields are equal when user first signs up or changes his password from his profile, but not in other cases e.g. updates his profile.

   right now I am using a quick and dirty hack by using booleans e.g. if(@skip_password_validations != false) #--do validations --#

  is there anything better than this. any help would be greatly appreciated.

Something like this:

  validates_presence_of :password, :unless =>
:skip_password_validations?
  validates_presence_of :password_confirmation, unless =>
:skip_password_validations?
  validates_confirmation_of :password, unless =>
:skip_password_validations?

  def skip_password_validations?
     @skip_password_validations
  end

Hi,

      I need to selectively turn off validations for example, I
want to
make sure that password and password_confirmation fields are equal
when user
first signs up or changes his password from his profile, but not in
other
cases e.g. updates his profile.

      right now I am using a quick and dirty hack by using booleans
e.g.
if(@skip_password_validations != false) #--do validations --#

     is there anything better than this. any help would be greatly
appreciated.

Something like this:

validates_presence_of :password, :unless =>
:skip_password_validations?
validates_presence_of :password_confirmation, unless =>
:skip_password_validations?
validates_confirmation_of :password, unless =>
:skip_password_validations?

def skip_password_validations?
    @skip_password_validations
end

or even
with_options :unless => skip_password_validations? do
  validates_presence_of :password
  validates_presence_of :password_confirmation
  validates_confirmation_of :password
end

Muhammad Ishaq wrote:

e.g.
if(@skip_password_validations != false) #--do validations --#

      is there anything better than this. any help would be greatly
appreciated.

What I'm doing looks like this:

validates_format_of :password, :with => /^\w+$/, :message => '<b style =
"color:red">Password must be alphanumeric</b>', :on => :save

validates_length_of :password, :minimum => 5, :message => '<b style =
"color:red">Password to short (Must be 5 characters long)</b>', :on =>
:create

Thanks a lot, that really helps cleaning the code a little

What about the :if parameter that validations can take?

Like this:

validates_format_of :password,
     :with => /^\w+$/,
     :message => '<b style ="color:red">Password must be alphanumeric</

',

     :on => :save
     :if => @skip_password_validations

The parameter can even be a Proc!