Hi all, Is there a smart way of hooking in to Rails'' validation framework without having a database model? I''m building a multi-part form which takes a URL on the first page, then downloads data from it and asks further questions on the next page, then does even more stuff on the next page. It doesn''t fit the Rails validation-on-model-create concept at all; is it possible to re-use at least some of the validation stuff in Rails or do I need to strike out on my own? Cheers, Simon
Rolling your own, or re-thinking why you are not using a model for this seems to be the current thought. I''ve asked this on this list, and IRC (IRC getting a seemingly /lot/ of requests for this lately). On 5/24/05, Simon Willison <cs1spw-+E0FUbDlCk21Qrn1Bg8BZw@public.gmane.org> wrote:> Hi all, > > Is there a smart way of hooking in to Rails'' validation framework > without having a database model? I''m building a multi-part form which > takes a URL on the first page, then downloads data from it and asks > further questions on the next page, then does even more stuff on the > next page. It doesn''t fit the Rails validation-on-model-create > concept at all; is it possible to re-use at least some of the > validation stuff in Rails or do I need to strike out on my own? > > Cheers, > > Simon > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 25 May 2005, at 04:41, Michael Campbell wrote:> Rolling your own, or re-thinking why you are not using a model for > this seems to be the current thought. I''ve asked this on this list, > and IRC (IRC getting a seemingly /lot/ of requests for this lately).Sounds like an itch waiting to be scratched... For the moment I''ve rolled my own, which was relatively painful as I felt I was reinventing the wheel for a lot of it - and I still haven''t got the old form data re-displaying in the form fields when a form is redisplayed. There are plenty of perfectly valid reasons to want to validate a form without directly using a model. In this case, my application interface provides the user with a text field in to which they enter a URL. The application then downloads a file from that URL and, assuming the download was successful, parses the file to extract some information from it. The next page displays this information to the user and asks them to confirm (or alter it) before the application stores it all in a database table. It might, just, be possible to hack this in to the model class, but doing so would almost certainly break the same model when used for the generic admin form for that object as the validation rules for on- create would be different for the two different ways of inserting data. This seems to me to be one of the few missing pieces of the Rails puzzle. I can think of tons of reasons to want to validate a form without putting the information submitted in to a database table - any "Advanced search" feature for example, or booking search systems like the ones used by airlines / Expedia etc. Cheers, Simon Willison
Simon Willison wrote:> There are plenty of perfectly valid reasons to want to validate a form > without directly using a model.I tried to use ActiveRecord::Validations module in a class which is not ActiveRecord::Base descendant. That should be working as DDH wrote: http://one.textdrive.com/pipermail/rails/2004-December/001144.html But I''m getting errors. Does anyone manage to use ActiveRecord::Validations out of ActiveRecord::Base descendant? Thanks, Igor
On 25-May-05, at 2:53 AM, Igor Anic wrote:> Does anyone manage to use ActiveRecord::Validations out of > ActiveRecord::Base descendant?Yes. I wish I could say that I came up with this myself but I spotted it in someone''s email a few months back - I can''t recall who originally posted hit but a tip-of-the-hat to whoever it was. I added the "human_attribute_name" call so that "error_messages_for" would work. Hope this helps, Trevor [in models/validating_base.rb] class ValidatingBase def save; end def update_attribute; end def new_record?; end def self.human_attribute_name(arg) arg.to_s end include ActiveRecord::Validations def [](key) instance_variable_get(key) end end [in models/something.rb] class Something < ValidatingBase attr_accessor :foo validates_length_of :foo, :within => 5..8 end [ in a controller somewhere...] s = Something.new s.foo = "wibble" s.valid?
On 25 May 2005, at 16:43, Trevor Squires wrote:> On 25-May-05, at 2:53 AM, Igor Anic wrote: >> Does anyone manage to use ActiveRecord::Validations out of >> ActiveRecord::Base descendant? > > Yes. > > I wish I could say that I came up with this myself but I spotted it > in someone''s email a few months back - I can''t recall who > originally posted hit but a tip-of-the-hat to whoever it was.Thank you thank you thank you! I''ve really been needing this. Could you add it to the Ruby on Rails wiki? Thanks, Simon
On 25-May-05, at 8:58 AM, Simon Willison wrote:> On 25 May 2005, at 16:43, Trevor Squires wrote: >> On 25-May-05, at 2:53 AM, Igor Anic wrote: >>> Does anyone manage to use ActiveRecord::Validations out of >>> ActiveRecord::Base descendant? >> >> Yes. >> >> I wish I could say that I came up with this myself but I spotted it >> in someone''s email a few months back - I can''t recall who originally >> posted hit but a tip-of-the-hat to whoever it was. > > Thank you thank you thank you! I''ve really been needing this. Could > you add it to the Ruby on Rails wiki?Sure - I''ll do that later today. Trevor