Forum: Ruby on Rails Overriding 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.
Steven R. (Guest)
on 2006-05-23 10:39
(Received via mailing list)
Is there a "normal" way to override the behavior of validator methods
(like validates_presence_of) for updates vs. creation?  For example,
in the AWDR example store program, the User class validates the
presence of the password and its length - which makes sense when the
user entry is created. But what if you wanted to provide an update
form that updates just certain items without validators (say, a
customer email address) while leaving the password unchanged ?
Cutting around update_attributes seems like a hack, so wondering if
there's a typical way to handle this.

Thanks,
SR
Calle D. (Guest)
on 2006-05-23 10:42
(Received via mailing list)
>>>>> "Steven" == Steven R. <removed_email_address@domain.invalid> writes:

> Is there a "normal" way to override the behavior of validator methods
> (like validates_presence_of) for updates vs. creation?

Yes, you use the "on" option as described in the documentation. Or the
"if" option described in the same place, if your circumstances are
more complex than just create/update.
--
		     Calle D. <removed_email_address@domain.invalid>
		 http://www.livejournal.com/users/cdybedahl/
	"Just about anything can be done if you are demented enough."
		-- Christopher C. Petro, scary.devil.monastery
Bill W. (Guest)
on 2006-05-23 18:06
(Received via mailing list)
Hi Steven,

Steven R. writes:
>
> Is there a "normal" way to override the behavior of validator methods
> (like validates_presence_of) for updates vs. creation?

The approach Calle recommended puts control of the validation within the
model which is where it typically belongs and which, it sounds like, is
appropriate to your situation.  I recently had what I'd consider a
fairsituation where I needed to create an empty record (i.e., without
doing
any validation) and had difficulty getting the :on => :update  there's
the
additional option of using Object.save_with_validations(false)
This topic is locked and can not be replied to.