Validations without AR - going crazy trying to find link


#1

Hi,

in the past few months someone posted an entry on their blog about
how to do validations in non-AR classes and I cant find it any more.

Anyone have a link?

Thanks,
Trevor


Trevor S.
http://somethinglearned.com


#2

I can’t find the original blog that I read a while ago but I use it like
this:

Make a ValidatingBase that the non-AR classes inherit from.

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
instance_variable_get(key)
end
include ActiveRecord::Validations
def initialize(attributes = nil)
return nil if attributes.nil?

attributes.each do |key, value|
  method("#{key}=").call(value)
end

end
end

class Example < ValidatingBase
attr_accessor :request_postcode, :request_telephone_number
validates_presence_of :request_telephone_number
protected
def validate
errors.add(:request_postcode, “must be a valid postcode”) unless
request_postcode.blank? || request_postcode =~
/\b[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]?
{1,2}[0-9][ABD-HJLN-UW-Z]{2}/
end
end

Hope this helps,

David

On Wed, 2006-03-01 at 09:24 -0800, Trevor S. wrote:

Hi,

in the past few months someone posted an entry on their blog about
how to do validations in non-AR classes and I cant find it any more.

Anyone have a link?

Thanks,
Trevor


David Blundell
100 Percent IT


#3

Hi David,

well, you have no idea how embarrassing it is but the code you gave
me is the same code uploaded to the wiki ages ago.

http://wiki.rubyonrails.org/rails/pages/
HowToUseValidationsWithoutExtendingActiveRecord

And wouldn’t you know it, someone updated that page with a link to
the (different) technique I was looking for - the one from technoweenie.

Thanks for your help, it got me there in the end.

Regards,
Trevor

On 1-Mar-06, at 9:39 AM, David Blundell wrote:

def self.human_attribute_name(arg)
attributes.each do |key, value|
errors.add(:request_postcode, “must be a valid postcode”)
unless request_postcode.blank? || request_postcode =~ /\b[A-PR-UWYZ]
[A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-
Z]{2}/
end
end

Hope this helps,

David

Trevor S.
http://somethinglearned.com


#4

Hi,

It may have been the following entry;

http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects

and the plugin is available via;

svn: http://www.realityforge.org/svn/code/active-form/trunk

On 3/2/06, Trevor S. removed_email_address@domain.invalid wrote:

Hi,

in the past few months someone posted an entry on their blog about
how to do validations in non-AR classes and I cant find it any more.

Anyone have a link?


Cheers,

Peter D.


#5

Hi Peter,

thanks for the pointer.

By the way (and in case you’re not already aware), when 1.1 comes out
you can remove the hacks to Dispatcher by doing:

class ActiveForm
include Reloadable::Subclasses

rest of class definition here

end

Regards,
Trevor


Trevor S.
http://somethinglearned.com


#6

Trevor S. wrote:

Hi,

in the past few months someone posted an entry on their blog about
how to do validations in non-AR classes and I cant find it any more.

Anyone have a link?

Thanks,
Trevor


Trevor S.
http://somethinglearned.com

I derive table-less models from this AR class (based on the tip posted
by techno-weenie):

8<-----------------------------------------------------

Memory based table-less model to validate input forms that don’t have

an

underlying table.

These act like strongly typed structures and inherit ActiveRecords

validations which make them very useful for form validation.

Based on tip by technoweenie:

http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model

class TransientRecord < ActiveRecord::Base

def initialize(attributes=nil)
super
# Explicit assignment of the id attribute is necessary because the
default
# ActiveRecord (1.0.0) behavior is to omit id from the @attributes
hash
# when a new record is instantiated. Until the id entry is added to
# @attributes with an explicit assignment it won’t be processed by
the
# attributes or attributes= methods.
self.id = nil if self.class.columns_hash[‘id’]
self.attributes = attributes if attributes
end

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

Because super does not process id attribute (see note in

initialize).
def attributes=(attributes)
attributes.each { |k,v| self[k] = v }
end

end
8<-----------------------------------------------------

Derive a concrete table-less model with something like, for example:

class ContactSearch < TransientRecord

column :id, :integer
column :first_name, :string
column :last_name, :string
column :from, :date
column :to, :date

validates_numericality_of :id, :allow_nil => true
:
end

Use it in your controller with something like, for example:

@contact_search = ContactSearch.new(params[:contact_search])
if not @contact_search.valid?
render :action => ‘search’
else
:

Cheers, Stuart

Stuart R.