Hi, I have a simple address form. All I want to do is to check the existence of State field if one of the addresses is set to US. I tried this but didn''t work validates_each :billing_address_state, :shipment_address_state do | model,attr, value | if :billing_address_country != "US" and value == "" model.errors.add(attr,"cannot be left empty") if :billing_address_country end My question is, how can I access instance variables within a validation helper block for validations based on the value of another attribute? Thanks in advance
Francois Beausoleil
2005-Oct-13 05:40 UTC
Re: accessing instance variable from validation helper.
Hi ! 2005/10/12, Oyku Gencay <oyku@gencay.net>:> validates_each :billing_address_state, :shipment_address_state do > | model,attr, value | > if :billing_address_country != "US" and value == "" > model.errors.add(attr,"cannot be left empty") > if :billing_address_country > end > > > My question is, how can I access instance variables within a > validation helper block for validations based on the value of another > attribute?model.another_attribute ? You have access to the full model, it's the first parameter in the block :) Bye ! François _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Silly me :) The code sample mislead me. I thought that the model was the class not the instance, which the other way around does not even make sense. Thanks a lot. Oyku On 13.Eki.2005, at 08:40, Francois Beausoleil wrote:> Hi ! > > 2005/10/12, Oyku Gencay <oyku-EH/T/IZ+b7usTnJN9+BGXg@public.gmane.org>: > >> validates_each :billing_address_state, :shipment_address_state do >> | model,attr, value | >> if :billing_address_country != "US" and value == "" >> model.errors.add(attr,"cannot be left empty") >> if :billing_address_country >> end >> >> >> My question is, how can I access instance variables within a >> validation helper block for validations based on the value of another >> attribute? >> > > model.another_attribute ? > > You have access to the full model, it''s the first parameter in the > block :) > > Bye ! > François > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >