Forum: Ruby on Rails skip validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pradeep M. (Guest)
on 2007-05-25 19:00
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
Faisal N Jawdat (Guest)
on 2007-05-25 19:09
(Received via mailing list)
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
Pradeep M. (Guest)
on 2007-05-25 19:22
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

4. Update user (no password field)

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

plz help me
MichaelLatta (Guest)
on 2007-05-25 21:41
(Received via mailing list)
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>
Robert W. (Guest)
on 2007-05-25 23:15
(Received via mailing list)
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.
Jet (Guest)
on 2007-05-26 17:29
(Received via mailing list)
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>
tonypm (Guest)
on 2007-08-16 10:35
(Received via mailing list)
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
This topic is locked and can not be replied to.