2006/4/16, Xavier L. [email protected]:
How can I achieve granular control over the validation for different
controllers? I create users from an administration controller, but I
don’t want to fill in all their information fields, just have their name
and email address. When they accept the invitation, I want to implement
validation on the other fields.
I have looked at using :on => :update, but I would have to make the user
update all their information on one page. Is there any way to associate
the validation with a controller action?
I had the same problem, and here’s how I did it:
class User < AR:B
validates_presence_of :phone, :if => :phone_required?
alias_method :phone_required?, :phone_required
In the controller, I simply set phone_required to true if I want the
phone to be validated. Of course, you could simplify by having a
single flag for a multiple fields.
Alternatively, do as Ashley said – split the separate fields in a
secondary table, and pur the validation on that model instead.
Hope that helps !