Xavier Lange
2006-Apr-16 21:18 UTC
[Rails] 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? -- Posted via http://www.ruby-forum.com/.
Ashley Moran
2006-Apr-16 22:08 UTC
[Rails] Granular model validation based around controller actions?
On Apr 16, 2006, at 10:18 pm, Xavier Lange wrote:> 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? >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
Bryan Duxbury
2006-Apr-16 23:51 UTC
[Rails] Re: Granular model validation based around controller action
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? > > AshleyOr, 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__. -- Posted via http://www.ruby-forum.com/.
Christian Klauser
2006-Apr-16 23:54 UTC
[Rails] Re: Granular model validation based around controller action
You could use callbacks like "after_find" and "before_validate" to delete / replace the __PLACEHOLDER__ values IF the user account is not validated. -- Posted via http://www.ruby-forum.com/.
Francois Beausoleil
2006-Apr-17 03:04 UTC
[Rails] Granular model validation based around controller actions?
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 ! -- Fran?ois Beausoleil http://blog.teksol.info/
Alain Ravet
2006-Apr-17 06:10 UTC
[Rails] Re: Granular model validation based around controller actions?
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