Hi, I just worked on getting validations for non-ActiveRecord model objects working. I essentially implemented an ActiveForm object that you can subclass for your own non-database form objects. The file is available from my blog [1] and I wouldn''t mind some feedback on whether my assumptions are right. It seems to work for me and makes it much easier to write non-database bound forms. [1] http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects -- Cheers, Peter Donald
On 12/2/05, Peter Donald <peter.j.donald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I just worked on getting validations for non-ActiveRecord model > objects working. I essentially implemented an ActiveForm object that > you can subclass for your own non-database form objects. The file is > available from my blog [1] and I wouldn''t mind some feedback on > whether my assumptions are right. It seems to work for me and makes it > much easier to write non-database bound forms. > > [1] http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects > > -- > Cheers, > > Peter DonaldHere''s my approach to the problem. Instead of making my own class and reimplementing the validations, I just removed the code that grabs the table info: class Contact < ActiveRecord::Base def self.columns() @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) end column :name, :string column :city, :string column :state, :string column :phone, :string column :email_address, :string validates_presence_of :name, :email_address end It''s still technically a model, so you can call #save on it. But just calling valid? works fine. http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model -- rick http://techno-weenie.net
Hi, It turns out that there is a wiki page[1] that already describes this technique. Who knew? :) It is a little dated but I wish I had wiki''ed before attempting this ;) [1] http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord On 12/3/05, Peter Donald <peter.j.donald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I just worked on getting validations for non-ActiveRecord model > objects working. I essentially implemented an ActiveForm object that > you can subclass for your own non-database form objects. The file is > available from my blog [1] and I wouldn''t mind some feedback on > whether my assumptions are right. It seems to work for me and makes it > much easier to write non-database bound forms. > > [1] http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects > > -- > Cheers, > > Peter Donald >-- Cheers, Peter Donald
On 12/5/05, Peter Donald <peter.j.donald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > It turns out that there is a wiki page[1] that already describes this > technique. Who knew? :) It is a little dated but I wish I had wiki''ed > before attempting this ;) > > [1] http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecordI knew about that, but my version doesn''t mess with rails as much. I prefer my 3 lines to set it up vs changes in the dark corners of the dispatcher libs. YMMV -- rick http://techno-weenie.net