Hello, I have problem with custom validator which should check such situation: I have two fields text_field ''object'', ''others'' and text_field ''object'', ''others_area'' I "raise" error if others is nil but others_area is filled up also when others is not nil but others_area isn''t numeric If both fields are nil it''s ok i allow such situation. How can I do that in one correlated validator? Best regards, Adr --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Please attach some code or try explain better, because i don''t understand you --~--~---------~--~----~------------~-------~--~----~ 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 Nov 14, 2:30 pm, jprogramista <jprogrami...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > I have problem with custom validator which should check such > situation: > I have two fields text_field ''object'', ''others'' and text_field > ''object'', ''others_area'' > I "raise" error if others is nil but others_area is filled up also > when others is not nil but others_area isn''t numeric > If both fields are nil it''s ok i allow such situation. > How can I do that in one correlated validator? > > Best regards, > AdrSimple. Use custom validation in your model of ''object''. Use something like this: class Bla < ActiveRecord::Base def validate if others.nil? and others_area.not_numeric? then errors.add("nasty thing", "happenned") end end end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
you need to write your own validation with validates_each something like: validates_each :others, :others_area, :on => :create, do |record, attr, value| unless others then #check for nil ... # do your validation here # and add error to record if necessary: record.errors.add :others, ''your message'' end end end -- 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 Nov 14, 10:43 am, Thorsten Muller <rails-mailing-l...@andreas- s.net> wrote:> you need to write your own validation with validates_each > > something like: > > validates_each :others, :others_area, :on => :create, do |record, attr,Hi thx for response, i tried to write way you described but stuck with such code: def validates_correlated_presence_of(*attr_names) send(validation_method(:create)) do |record| value1 = record.send(attr_names[0]) value2 = record.send(attr_names[1]) if(!value1.nil? || !value2.nil?) #validates_presence_of(attr_names[0], attr_names[1], :message => "must be filled") validates_presence_of(attr_names[0], :message => "must be filled") validates_presence_of(attr_names[1], :message => "must be filled") end end end Validator goes all lines of code but validates_presence_of(attr_names[0], :message => "must be filled") isn''t adding any errors to record object. Is it possible to embed one validator into another? Best regards, Adr --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---