Granular model validation based around controller actions?

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?

On Apr 16, 2006, at 10:18 pm, Xavier L. wrote:

update all their information on one page. Is there any way to
associate
the validation with a controller action?

Xavier

If I have understood you right, this sounds like a database design
issue. Could you put all the optional data in a separate
user_details table, and set a :has_one relationship in your users class?

Ashley

You could use callbacks like “after_find” and “before_validate” to
delete / replace the PLACEHOLDER values IF the user account is not
validated.

Ashley M. wrote:

On Apr 16, 2006, at 10:18 pm, Xavier L. wrote:

update all their information on one page. Is there any way to
associate
the validation with a controller action?

Xavier

If I have understood you right, this sounds like a database design
issue. Could you put all the optional data in a separate
user_details table, and set a :has_one relationship in your users class?

Ashley

Or, use one table, but put placeholder values in fields that won’t
validate when empty. You can then detect these placeholders when the
user goes to edit their profile and remove them before displaying the
form for editing. Make sure you use something really strange for the
placeholder, like PLACEHOLDER.

Ashley M. wrote:

put all the optional data in a separate user_details
table, and set a :has_one relationship in your users class?

Or use the same table in 2 models, 1 looser and 1 stricter :

class User < AR:B
end
class StricterUser < User
  validates_presence_of ..
  validates...
end

Alain

Hi !

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?
attr_accessor :phone_required
alias_method :phone_required?, :phone_required
end

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 !