Hi, I want to use my some session data when I validate som data in the model. The specific problem I have is that I present different forms data based on gender, and then dependent of the gender, there''s different fields that needs validation. I''m wondering what''s the preffered way of doing this. The session data is not present in the model, i.e: class Myclass < ActiveRecord::Base belongs_to :member def validate errors.add_on_empty %w{ required_field } if @session["member"].gender == ''m'' end end does not work since the @session["member"] variable is not set. I''ve tried to set: @member = @session["member"] in my controller before I call Myclass.save and that doesn''t seem to work either. So how should this be handled? Best regards //johan _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
If the gender is required for validation, it should probably be part of the model in a way or another. Thus,I would add a gender attribute to the model (not necessarily persisted) write a getter and a setter for it It probably is persisted or can be calculated so in the initilisation I would retrieve or calculate it accordingly to set the object initial state. Then the controller just has to set the new values including gender, and your validation uses the attribute value. jean On Mon, 28 Feb 2005 14:18:33 +1100, Johan Allard <johan-8aRrlto6guaHXe+LvDLADg@public.gmane.org> wrote:> Hi, > > I want to use my some session data when I validate som data in the > model. The specific problem I have is that I present different forms > data based on gender, and then dependent of the gender, there''s > different fields that needs validation. I''m wondering what''s the > preffered way of doing this. The session data is not present in the > model, i.e: > > class Myclass < ActiveRecord::Base > belongs_to :member > > def validate > errors.add_on_empty %w{ required_field } if @session["member"].gender > == ''m'' > end > end > > does not work since the @session["member"] variable is not set. I''ve > tried to set: > > @member = @session["member"] > > in my controller before I call Myclass.save and that doesn''t seem to > work either. So how should this be handled? > > Best regards > > //johan > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
Jean Helou wrote:> If the gender is required for validation, it should probably be part > of the model in a way or another.I can think of many cases where this doesn''t make sense; it''s only a hack to work around Rails'' missing support for model-independent form validation. I''m not sure how this validation should look like, maybe someone who knows other web frameworks can make a suggestion?
On 2005-02-28, at 20.36, Jean Helou wrote:> If the gender is required for validation, it should probably be part > of the model in a way or another.Well, it''s not really required for validation. It''s just they way that the model is, that some things applies to men only and some things apply to women only. If I want to validate the men only part I need to know the gender of the member. If we''re talking about different types of cancer for instant, it seems strange to add gender to the model.> Thus,I would add a gender attribute to the model (not necessarily > persisted) > write a getter and a setter for it > It probably is persisted or can be calculated so in the initilisation > I would retrieve or calculate it accordingly to set the object initial > state.Hmm, If I add: def gender @session["member"].gender end to the model, I''m no where closer to the solution since the validation is also handled within the same model and the @session variable won''t be set here either.> Then the controller just has to set the new values including gender, > and your validation uses the attribute value.I must be slow. How do you mean. I''ve already tried to set @gender = @session["member"].gender before calling the save (and validation) and that didn''t work. Best regards //johan> jean > > On Mon, 28 Feb 2005 14:18:33 +1100, Johan Allard <johan-8aRrlto6guaHXe+LvDLADg@public.gmane.org> > wrote: >> Hi, >> >> I want to use my some session data when I validate som data in the >> model. The specific problem I have is that I present different forms >> data based on gender, and then dependent of the gender, there''s >> different fields that needs validation. I''m wondering what''s the >> preffered way of doing this. The session data is not present in the >> model, i.e: >> >> class Myclass < ActiveRecord::Base >> belongs_to :member >> >> def validate >> errors.add_on_empty %w{ required_field } if >> @session["member"].gender >> == ''m'' >> end >> end >> >> does not work since the @session["member"] variable is not set. I''ve >> tried to set: >> >> @member = @session["member"] >> >> in my controller before I call Myclass.save and that doesn''t seem to >> work either. So how should this be handled? >> >> Best regards >> >> //johan >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Mon, 28 Feb 2005 23:29:55 +1100, Johan Allard <johan-8aRrlto6guaHXe+LvDLADg@public.gmane.org> wrote:> > On 2005-02-28, at 20.36, Jean Helou wrote: > > > If the gender is required for validation, it should probably be part > > of the model in a way or another. > > Well, it''s not really required for validation. It''s just they way that > the model is, that some things applies to men only and some things > apply to women only. If I want to validate the men only part I need to > know the gender of the member. If we''re talking about different types > of cancer for instant, it seems strange to add gender to the model. > > > Thus,I would add a gender attribute to the model (not necessarily > > persisted) > > write a getter and a setter for it > > It probably is persisted or can be calculated so in the initilisation > > I would retrieve or calculate it accordingly to set the object initial > > state. > > Hmm, If I add: > > def gender > @session["member"].gender > end > > to the model, I''m no where closer to the solution since the validation > is also handled within the same model and the @session variable won''t > be set here either. > > > Then the controller just has to set the new values including gender, > > and your validation uses the attribute value. > > I must be slow. How do you mean. I''ve already tried to set @gender > @session["member"].gender before calling the save (and validation) and > that didn''t work. > > Best regards > > //johanHi Johan A thought occured to me reading this thread; it may be way off as I just woke up 20 mins ago, but might as well give it some thought :) The approach that I would take as an initial attempt at this problem would be to create a ''generic'' Person class that extends from ActiveRecord::Base, then Man/Woman classes that extend from Person. You could then combine the common traits into the Person class, but have the more specific questions and/or validation in the more specific classes. Dave -- Dave Goodlad dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org http://david.goodlad.ca/
Hi, Johan Allard wrote:>> If the gender is required for validation, it should probably be part >> of the model in a way or another. > > > Well, it''s not really required for validation. It''s just they way that > the model is, that some things applies to men only and some things > apply to women only. If I want to validate the men only part I need to > know the gender of the member. If we''re talking about different types > of cancer for instant, it seems strange to add gender to the model. >hm, I was wondering if it was possible to perform the validation in the controller? If the information necessary to validate something is only available in the controller it seems appropriate to do the actual work there, instead of figuring out how to move this information into the model. I guess the only thing you need is a way to access the model''s error object so that you can add errors to it. I didn''t see anything, on a quick glance through the docs, though. Anyone? Sebastian