Hi all, I need to call validation methods from outside the models. This is a workflow application so the models don''t have validation methods declared in it. Example: class Step < ActiveRecord::Base end def Wrk < ActiveRecord::Base has_many :steps end I''ve tried using for instance Step.validates_numericality_of(...) or Wrk.validates_format_of(...) but it does not work. Can anyone help me? Best regards, Hugo -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I believe you are not thinking in the proper use of validations. What you want to do is: s = Step.new ... s.valid? s.errors.on(*attribute_to_be_validated*) #returns nil for a valid value. or the error message in case the validation was not met On Fri, Mar 26, 2010 at 2:36 AM, Hugo Mag <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi all, > > I need to call validation methods from outside the models. > This is a workflow application so the models don''t have validation > methods declared in it. > > Example: > class Step < ActiveRecord::Base > end > > def Wrk < ActiveRecord::Base > has_many :steps > end > > I''ve tried using for instance Step.validates_numericality_of(...) or > Wrk.validates_format_of(...) but it does not work. > > Can anyone help me? > Best regards, > Hugo > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Mahmoud Said Software Engineer - eSpace blog.modsaid.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Mahmoud, Thanks for the info but the problem here is that my model does not have any validations declared because they depend on the workflow that the user sets. For instance, the user can set that in the Step 1 of the Workflow he wants to validate that a given attribute is greater than 300. To do this I need to call directly the validates_numericality_of class method: Step.validates_numericality_of(:attribute, :greater_than => 300) Best regards, Hugo Mahmoud Said wrote:> I believe you are not thinking in the proper use of validations. > > What you want to do is: > > s = Step.new > ... > > s.valid? > s.errors.on(*attribute_to_be_validated*) #returns nil for a valid > value. or > the error message in case the validation was not met > > > > On Fri, Mar 26, 2010 at 2:36 AM, Hugo Mag <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> def Wrk < ActiveRecord::Base >> Posted via http://www.ruby-forum.com/. >> >> > > > -- > Mahmoud Said > Software Engineer - eSpace > blog.modsaid.com-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The validations method you''re trying to call is not a class method, it''s a validation helper so you can''t call object.validation_method (in this case Step.validates_numericality_of(...)). The best way would be to declare customs validations if you want to call them outside your class and then handle exception (begin/rescue etc.), you should also use object.errors.add(attribute, ''custom error message''). Cheers, Emré. On Mar 26, 7:43 am, Hugo Mag <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi Mahmoud, > > Thanks for the info but the problem here is that my model does not have > any validations declared because they depend on the workflow that the > user sets. > > For instance, the user can set that in the Step 1 of the Workflow he > wants to validate that a given attribute is greater than 300. > To do this I need to call directly the validates_numericality_of class > method: > > Step.validates_numericality_of(:attribute, :greater_than => 300) > > Best regards, > Hugo > > > > > > Mahmoud Said wrote: > > I believe you are not thinking in the proper use of validations. > > > What you want to do is: > > > s = Step.new > > ... > > > s.valid? > > s.errors.on(*attribute_to_be_validated*) #returns nil for a valid > > value. or > > the error message in case the validation was not met > > > On Fri, Mar 26, 2010 at 2:36 AM, Hugo Mag <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > >> def Wrk < ActiveRecord::Base > >> Posted viahttp://www.ruby-forum.com/. > > > -- > > Mahmoud Said > > Software Engineer - eSpace > > blog.modsaid.com > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
exactly like Emre told u. just keep in mind when u have to clear the errors if that depends on the state On Fri, Mar 26, 2010 at 8:09 PM, Emre <152809-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The validations method you''re trying to call is not a class method, > it''s a validation helper so you can''t call object.validation_method > (in this case Step.validates_numericality_of(...)). > > The best way would be to declare customs validations if you want to > call them outside your class and then handle exception (begin/rescue > etc.), you should also use object.errors.add(attribute, ''custom error > message''). > > Cheers, > Emré. > > On Mar 26, 7:43 am, Hugo Mag <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > Hi Mahmoud, > > > > Thanks for the info but the problem here is that my model does not have > > any validations declared because they depend on the workflow that the > > user sets. > > > > For instance, the user can set that in the Step 1 of the Workflow he > > wants to validate that a given attribute is greater than 300. > > To do this I need to call directly the validates_numericality_of class > > method: > > > > Step.validates_numericality_of(:attribute, :greater_than => 300) > > > > Best regards, > > Hugo > > > > > > > > > > > > Mahmoud Said wrote: > > > I believe you are not thinking in the proper use of validations. > > > > > What you want to do is: > > > > > s = Step.new > > > ... > > > > > s.valid? > > > s.errors.on(*attribute_to_be_validated*) #returns nil for a valid > > > value. or > > > the error message in case the validation was not met > > > > > On Fri, Mar 26, 2010 at 2:36 AM, Hugo Mag <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: > > > > >> def Wrk < ActiveRecord::Base > > >> Posted viahttp://www.ruby-forum.com/. > > > > > -- > > > Mahmoud Said > > > Software Engineer - eSpace > > > blog.modsaid.com > > > > -- > > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Mahmoud Said Software Engineer - eSpace blog.modsaid.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.