Greg Hauptmann
2007-May-23 03:05 UTC
Rails model validation: Can I create my own validations???
Hi, Is it possible to create my own validations within the Rails model validation framework? In particular I''m interesting in being able to create a global (i.e. not associated with a particular field) business logic validation exception(s) to pass back to the controller. They could then be listed on the View as an exception error, but not associated with a particular field. I''m thinking for higher level business logic rule exceptions like this it may be better doing it the above way as opposed to creating an exception ( e.g. businessLogic exception class) - but any ideas/comments welcome on this. Tks Greg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Keynan Pratt
2007-May-23 04:06 UTC
Re: Rails model validation: Can I create my own validations?
all rails model validators (ie. validates_uniqueness_of) are based on validates_each so all you have to do is def self.validates_something(*args) validates_each(*args){|column| errors.add(blah blah blah) if not something } 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 -~----------~----~----~----~------~----~------~--~---
MichaelLatta
2007-May-23 16:38 UTC
Re: Rails model validation: Can I create my own validations???
You can add any validation logic you like to the validate method on any active model class. It can validate any combination of attributes of the object. If you are wanting to add new meta-programming methods like validate_presence_of that takes some additional digging I have not done yet. Michael On May 22, 8:05 pm, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Is it possible to create my own validations within the Rails model > validation framework? > > In particular I''m interesting in being able to create a global (i.e. not > associated with a particular field) business logic validation exception(s) > to pass back to the controller. They could then be listed on the View as an > exception error, but not associated with a particular field. > > I''m thinking for higher level business logic rule exceptions like this it > may be better doing it the above way as opposed to creating an exception ( > e.g. businessLogic exception class) - but any ideas/comments welcome on > this. > > Tks > Greg--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
George
2007-May-24 03:20 UTC
Re: Rails model validation: Can I create my own validations???
On 5/24/07, MichaelLatta <lattam-ee4meeAH724@public.gmane.org> wrote:> > If you are wanting to add new meta-programming methods like > validate_presence_of that takes some additional digging I have not > done yet. > > MichaelMost of those just wrap #validates_each, which is documented. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2007-May-24 06:52 UTC
Re: Rails model validation: Can I create my own validations???
How does this look (I''m not at my rails PC at the moment)? i.e. regarding the use of Global (not attribute specific) validations within Model --------------------------------------------------------- class Person < ActiveRecord::Base protected def validate_on_create if !complex_business_logic_passes? errors.add_to_base("complex business logic failed") end end def complex_business_logic_passes? ...<put complex business logic here> return result # true if passes end end --------------------------------------------------------- <body> Business Logic Errors = <%= error_message_on "object", "base" %> </body> --------------------------------------------------------- On 5/24/07, George <george.ogata-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 5/24/07, MichaelLatta <lattam-ee4meeAH724@public.gmane.org> wrote: > > > > If you are wanting to add new meta-programming methods like > > validate_presence_of that takes some additional digging I have not > > done yet. > > > > Michael > > Most of those just wrap #validates_each, which is documented. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---