Skip validation


#1

Hi all,

In user model i have 4 validations. In an update i need to skip 2 of
those validations and other 2 need to fire.

Please help how to do that.

Thanks in advance


#2

On May 25, 2007, at 11:00 AM, Pradeep M. wrote:

In user model i have 4 validations. In an update i need to skip 2
of those validations and other 2 need to fire.

:on => create

-faisal


#3

Faisal N Jawdat wrote:

On May 25, 2007, at 11:00 AM, Pradeep M. wrote:

In user model i have 4 validations. In an update i need to skip 2
of those validations and other 2 need to fire.

:on => create

-faisal

Thanks for your response…

let me give detail information

i have validations on email, password, password length etc…

i have the following methods

  1. signup
  2. forgot password
  3. change password

In the above 3 scenarios i am using save and update methods here both
validations should exist

  1. Update user (no password field)

In this scenario i need to skip the password validation for length

plz help me


#4

First a more direct answer to your question:

ActiveRecord supplies validate_on_create and validate_on_update as
separate methods from validate. If you really wanted validation of
creation of objects, but not on update you could put your validation
in validate_on_create.

However, it sounds like you have a fundamental misunderstanding of
validation (as eluded to in a previous reply). If you are validating
a user’s password for presence and length then you should always
perform that check.

class User < ActiveRecord::Base
validates_presence_of :username, :password
validates_length_of :password, :minimum => 6

validate_on_create
#put your create validations here
end

validate_on_update
#put your update validations here
end
end

It almost sounds like you are validating somewhere else besides the
model. If you are doing that then…well don’t. What happens if
records are inserted or updated without going through your view or
controller.


#5

This does not sound right to me.

WHen you save a user object you should either have a password or not
have one. If you allow the user to exist with no password then your
validations should be done conditionally in your validate method.
But, I would not allow a user to exist without a password, that is a
bad security setup. Validations happen at the model level not the
form level, so you are validating the entire user record not just the
fields being changed by a form.

If I misunderstood your situation pleas include more detail.

Michael

On May 25, 8:22 am, Pradeep M. removed_email_address@domain.invalid


#6

Hi
from what i have understood:
firstly u need to get a instance of the user you want to update
@user = users.find( :first, :conditions => { :id => your id} )
now update the instance and call update on this instance
the validations are already in the right place so no need to change
them

cheers…

On May 25, 8:22 am, Pradeep M. removed_email_address@domain.invalid


#7

Hi,

I appreciate that this is an old post, but I have just come across
what I think is the same situation. I have a role attribute in my
user, and allow the role to be changed. To allow this, I was using
update_attribute_with_validation_skipping, so that the lack of
confirmation password field error would not occur. Since role was
coming from a select, this was fairly ok.

However, I now want to add other attributes which the admin user needs
to be able to change. It would be nice to add validation to these
fields, but then I cannot use validation skipping.

I just wondered if you had come up with a neat solution, or if others
had any ideas. The two possibilities I can see are:

a. Continue to use validation skipping and write my own validation
checks in the controller (or in the model, but I am not sure how I
would trigger them there).
b. Put the additional attributes in a separate ‘has and belongs to
one’ model.

Thanks
Tonypm