# Validation Non Activerecord Objects

**URL:** <https://www.ruby-forum.com/t/validation-non-activerecord-objects/66142>\
**Category:** Rails\
**Created:** [August 23, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/validation-non-activerecord-objects/66142 "2006-08-23T16:51:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 23, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/validation-non-activerecord-objects/66142/1 "2006-08-23T16:51:40Z")

</div>

Hi,

I know there are ways to validation non active record objects from the  
link below.

[http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord](http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord)

In activerecord validations when a field fails validation it is  
highlighted in red and the name of the field appears in the error log at  
the top of the page. Is it possible to use this functionality with non  
active record objects or do you have to write your own?

Any suggestions appreciated?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 23, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/validation-non-activerecord-objects/66142/2 "2006-08-23T17:15:46Z")

</div>

I think you can declare a validate method in your model and use  
errors.addto add messages for your view. Not exactly sure though.

-m

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 23, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/validation-non-activerecord-objects/66142/3 "2006-08-23T18:40:01Z")

</div>

@John:

See this little writeup I did a while back… uses an example from Rick  
Olson’s blog and expands upon it. This should give you exactly what you  
need.

[http://www.bphogan.com/learn/pdf/tableless\_contact\_form.pdf](http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [October 31, 2006, 11:39am UTC](https://www.ruby-forum.com/t/validation-non-activerecord-objects/66142/4 "2006-10-31T11:39:24Z")

</div>

> def method\_missing(symbol, @params)

Shouldn’t @params be \*params ?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 29, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/validation-non-activerecord-objects/66142/5 "2006-08-29T22:29:28Z")

</div>

There is a chapter in the Rails Recipes book on this. Create a  
validateable.rb file and add:

module Validateable  
[:save, :save!, :update\_attribute].each{|attr| define\_method(attr){}}  
def method\_missing(symbol, @params)  
if(symbol.to\_s =~ /(.\*)\_before\_type\_cast$/)  
send($1)  
end  
end  
def self.append\_features(base)  
super  
base.send(:include, ActiveRecord::Validations)  
end  
end

then in your model:

class Person  
include Validateable  
attr\_accessor :age  
validates\_numericality\_of :age  
end

you can take advantage of the valid?() method now, and also use  
error\_messages\_on() and error\_messages\_for() helpers

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 1, 2006, 7:00am UTC](https://www.ruby-forum.com/t/validation-non-activerecord-objects/66142/6 "2006-11-01T07:00:07Z")

</div>

I’m using this and it works well, and is nicely encapsulated.

[http://www.agilewebdevelopment.com/plugins/active\_form](http://www.agilewebdevelopment.com/plugins/active_form)

Wes
