Forum: Ruby on Rails Granular model validation based around controller actions?

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.
3ac87d9d01538788709a428a67a63e96?d=identicon&s=25 Xavier Lange (Guest)
on 2006-04-16 23:18
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?
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-17 00:10
(Received via mailing list)
On Apr 16, 2006, at 10:18 pm, Xavier Lange 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
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-04-17 01:50
Ashley Moran wrote:
> On Apr 16, 2006, at 10:18 pm, Xavier Lange 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__.
A1c4a0bb61649f18a4d65b1c7273a50f?d=identicon&s=25 Christian Klauser (sealedsun)
on 2006-04-17 01:54
You could use callbacks like "after_find" and "before_validate" to
delete / replace the __PLACEHOLDER__ values IF the user account is not
validated.
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-04-17 05:04
(Received via mailing list)
Hi !

2006/4/16, Xavier Lange <D_Lange21@sbcglobal.net>:
> 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 !
0091f92762685860109bbcb02edfdf27?d=identicon&s=25 Alain Ravet (Guest)
on 2006-04-17 08:13
(Received via mailing list)
Ashley Moran 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
This topic is locked and can not be replied to.