Overriding validation?


#1

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


#2

“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

#3

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)