[Rails-core] Validate absence of
Hello Core, I often need to validate the absence of active record fields. That validation being the direct opposite of validating the presence of a field. Would the core team be willing to accept a patch to that effect? Thanks, Paul -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Re: [Rails-core] Validate absence of
On Mon, Feb 27, 2012 at 10:15 AM, Paul Gillard wrote: > I often need to validate the absence of active record fields. That > validation being the direct opposite of validating the presence of a > field. Would the core team be willing to accept a patch to that > effect? Interesting! Can you explain some uses cases? If it makes sense I think it could be added. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Re: [Rails-core] Validate absence of
On Mon, February 27, 2012 04:18, Xavier Noria wrote: > On Mon, Feb 27, 2012 at 10:15 AM, Paul Gillard > wrote: > >> I often need to validate the absence of active record >> fields. That >> validation being the direct opposite of validating the >> presence of a >> field. Would the core team be willing to accept a patch >> to that >> effect? > > Interesting! Can you explain some uses cases? > > If it makes sense I think it could be added. > Bot trapping with hidden fields that no human would complete? -- *** E-Mail is NOT a SECURE channel *** James B. Byrnemailto:byrn...@harte-lyne.ca Harte & Lyne Limited http://www.harte-lyne.ca 9 Brockley Drive vox: +1 905 561 1241 Hamilton, Ontario fax: +1 905 561 0757 Canada L8E 3C3 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Re: [Rails-core] Validate absence of
I believe that this one was a bad example... I found myself implementing this a couple times in the last year: validate :a_or_b > > def a_or_b > if a? && b? > self.errors.add :a, "You can't choose both options at the same time." > end > end > One of the cases was extremely common: Choose something: [ ] Option 1 [ ] Option 2 [ ] Option 3 [ ] Other Each option would be an *option_id : integer*, and Other would be *other_option : string* . For that case : validates :option_id, :absence => true, :if => :other_option? validates :other_option, :absence => true, :if => :option_id? Would fit perfectly! (Considering that at least one of them is obrigatory). What do you think ? -- Att, Everton -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Re: [Rails-core] Validate absence of
Assume you have a tree, where you want to have set attributes only, if a node is a root. Otherwise the model should not accept entries for a specific attribute. I.e. if you have a tree of categories and want to have a locale attribute only on roots you could do s'thing like this: class Category < ActiveRecord::Base validates :locale, :presence => true, :if => :root? validates :locale, :absence => true, :unless => :root? end Actually I'm doing this a lot. I'll use s'thing else, if someone has a better pattern. Cheers, Roberto -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/_Qgt0vhDA4wJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Re: [Rails-core] Validate absence of
I just worked this into rails and submitted a pull request: https://github.com/rails/rails/pull/7155 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/WqPA-wN07LkJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.