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.
9ac2db57875d22b54bcd9618bb171b21?d=identicon&s=25 Steven Rogers (srogers)
on 2006-05-23 08: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
9d7d8ef2179661d6b30e180fa588cd45?d=identicon&s=25 Calle Dybedahl (Guest)
on 2006-05-23 08:42
(Received via mailing list)
>>>>> "Steven" == Steven Rogers <srogers1@austin.rr.com> 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 Dybedahl <calle@cyberpomo.com>
		 http://www.livejournal.com/users/cdybedahl/
	"Just about anything can be done if you are demented enough."
		-- Christopher C. Petro, scary.devil.monastery
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-05-23 16:06
(Received via mailing list)
Hi Steven,

Steven Rogers 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.