The Joyent Community

A place where the Joyent community can gather, help each other out, and stay informed.

You are not logged in.

#1 2006-04-02 18:33:53

applepro
Member
From: London, UK
Registered: 2005-08-05
Posts: 129
Expertise

Rails form validation without having to create model and table?

I'm creating a form that people will fill in. I want to use Active Record style validation, e.g. validate_numericality and so on, but without having to create a model, and table in the database, as this this in only then going to be e-mailed using Action Mailer.

Any ideas? How would you do it? Or is creating a model, table, etc. the only route, I guess apart from sticking some regexps in the action method.

Thanks,

Roman

Last edited by applepro (2006-04-02 18:34:22)


blackboxbackup.com - continuous encrypted offsite backup

Offline

 

#2 2006-04-02 19:04:58

misterk
?
From: Mpls, MN
Registered: 2004-06-01
Posts: 1167
Website  Expertise

Re: Rails form validation without having to create model and table?

I did this awihle back and duped Active Record into thinking there was a model:

Code:

ActiveRecord::Base.class_eval do

alias_method :save, :valid? 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, null) end
end
class Wishlist < ActiveRecord::Base # Add whichever columns you might need here, they will be mocked # as ActiveRecord database rows. COLUMNS = [ :id, :from, :from_name, :to, :body, :products ] # Extension of AR::Base above lets me mock these database columns, # allowing me to have AR goodness without a real database. self.class_eval do COLUMNS.each { |c| column("#{c}", :string) } end


-kjell

Offline

 

#3 2006-04-02 21:25:29

dabux
Member
Registered: 2005-09-06
Posts: 85
Expertise

Re: Rails form validation without having to create model and table?

here is another option that i came across. don't remember from where. Add active_form.rb to your models dir.

Code:

# Note ".valid?" method  must occur on object for validates_associated
class ActiveForm

def initialize(attributes = nil) if attributes attributes.each do |key,value| send(key.to_s + '=', value) end end yield self if block_given? end def [](key) instance_variable_get("@#{key}") end def method_missing( method_id, *args ) if md = /_before_type_cast$/.match(method_id.to_s) attr_name = md.pre_match return self[attr_name] if self.respond_to?(attr_name) end super end

protected

def raise_not_implemented_error(*params) ValidatingModel.raise_not_implemented_error(*params) end def self.human_attribute_name(attribute_key_name) attribute_key_name.humanize end def new_record? true end # these methods must be defined before include alias save raise_not_implemented_error alias update_attribute raise_not_implemented_error

public

include ActiveRecord::Validations

protected

# the following methods must be defined after include so that they overide # methods previously included alias save! raise_not_implemented_error class << self def raise_not_implemented_error(*params) raise NotImplementedError end alias validates_uniqueness_of raise_not_implemented_error alias create! raise_not_implemented_error alias validate_on_create raise_not_implemented_error alias validate_on_update raise_not_implemented_error alias save_with_validation raise_not_implemented_error end
end

require 'dispatcher'
class Dispatcher

class << self if ! method_defined?(:form_original_reset_application!) alias :form_original_reset_application! :reset_application! def reset_application! form_original_reset_application! Dependencies.remove_subclasses_for(ActiveForm) if defined?(ActiveForm) end end end
end


then here is how you use it:

Code:

class ContactEmail < ActiveForm

attr_accessor :email, :subject, :body validates_presence_of :email, :subject, :body validates_format_of :email, :with => /[-!#$&'*+\/=?`{|}~.\w]+@[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*(\.[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*)+$/, :message => ' appears to be invalid'
end

Offline

 

#4 2006-04-18 20:59:58

sunblaze
New member
Registered: 2006-04-18
Posts: 1
Expertise

Re: Rails form validation without having to create model and table?

This answered the exact same question I had. Thanks a bunch.

The only thing, the second solution works great except for one part.

Code:

require 'dispatcher'
class Dispatcher

class << self if ! method_defined?(:form_original_reset_application!) alias :form_original_reset_application! :reset_application! def reset_application! form_original_reset_application! Dependencies.remove_subclasses_for(ActiveForm) if defined?(ActiveForm) end end end

end


I have no clue what this does. I had to comment it out since it seemed to screw up my rails app. With it after something referenced the ActiveForm object my rails app would no longer respond to valid controller actions.

Anyways thanks for the help.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson