Hi. I''m trying to do a pre-validation in a form. What I need is to do some validations, then, depending on this validation, ask a couple of questions to the user and to process the form, including the user''s answers. For instance Name: John Last name: Doe Country: Mars I need to validate the existence of the country but just to give a warning to the user. Something like "The country is not on our database. Some features won''t be available for you. Do you want to proceed?" And a yes/no checkbox. I did a custom validation in the controller and it works but, since I''m not using the ActiveRecord validation way, the values of the form are not retained. Is there an ActiveRecord/Rails/Elegant way of doing this? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I did a custom validation in the controller and it works but, since I''m > not using the ActiveRecord validation way, the values of the form are > not retained.why don''t you use the custom validation? in your view you could check with myrecord.errors.on(:country) if there''s an error on country and show the checkbox if necessary> > Is there an ActiveRecord/Rails/Elegant way of doing this? >yes. use the normal way if that''s not possible, you can keep the form values by generating a new record (without saving) and filling a form (maybe invisible) with the values, so they''re handed back again later -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote:> > in your view you could check with myrecord.errors.on(:country) if > there''s an error on country and show the checkbox if necessaryMaybe I''m wrong, but I think if I do it that way (using myrecord.errors), the validation would prevent me from saving the record. Usually that''s the expected behaviour but in my case I want the validation just for a warning. Thanks for your answer. Greetings. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 1/2/08, Fernando Poblete <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi. I''m trying to do a pre-validation in a form. What I need is to do > some validations, then, depending on this validation, ask a couple of > questions to the user and to process the form, including the user''s > answers. > > For instance > > Name: John > Last name: Doe > Country: Mars > > I need to validate the existence of the country but just to give a > warning to the user. Something like > > "The country is not on our database. Some features won''t be available > for you. Do you want to proceed?" > And a yes/no checkbox. > > I did a custom validation in the controller and it works but, since I''m > not using the ActiveRecord validation way, the values of the form are > not retained. > > Is there an ActiveRecord/Rails/Elegant way of doing this?Most of the validates_* methods accept an :if parameter where you can place your conditional logic. -- Greg Donald http://destiney.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Most of the validates_* methods accept an :if parameter where you can > place your conditional logic. >They do, but how can I include a checkbox as a validation''s response. Currently, I''m doing def validate if self.country.not_exist? errors.add("Country does not exist. Do you want to proceed?" + "OK<input id=''contact_proceed'' name=''contact[proceed]'' type=''checkbox''" + "value=''1'' /><input name=''contact[proceed]'' type=''hidden'' value=''0'' />" end end Actually, it''s working but I don''t like at all this implementation so I''m still hoping a better solution. Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 1/3/08, Fernando Poblete <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Most of the validates_* methods accept an :if parameter where you can > > place your conditional logic. > > > > They do, but how can I include a checkbox as a validation''s response.You have get your checkbox''s value into the model. I do this by merging the object into the params: params[:foo].merge( :foo => foo ) Then inside the model add an :if to the validation, something like this: :if => Proc.new { |model| model.foo.checkbox_fieldname == 1 } -- Greg Donald http://destiney.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---