# Validation in Radiant

**URL:** <https://www.ruby-forum.com/t/validation-in-radiant/123930>\
**Category:** Radiant CMS\
**Created:** [December 22, 2007, 5:34am UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930 "2007-12-22T05:34:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 22, 2007, 5:34am UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/1 "2007-12-22T05:34:04Z")

</div>

Does anyone knows if normal ROR validation could be done inside of  
Radiant extensions. I’ve tried regular ROR validation methods like  
validates\_presence\_of inside of my model but it does’t work. Does anyone  
have an idea on how to do that.

Thanks

Maged M.

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 22, 2007, 6:28am UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/2 "2007-12-22T06:28:01Z")

</div>

On Dec 21, 2007, at 11:34 PM, Maged M. wrote:

> Does anyone knows if normal ROR validation could be done inside of  
> Radiant extensions. I’ve tried regular ROR validation methods like  
> validates\_presence\_of inside of my model but it does’t work. Does  
> anyone  
> have an idea on how to do that.

Validation is a feature of ActiveRecord so it should work flawlessly  
in a Radiant extension. Do you need to restart your mongrels? Do you  
have a parse error in your models?

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 22, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/3 "2007-12-22T20:08:11Z")

</div>

John W. Long wrote:

> On Dec 21, 2007, at 11:34 PM, Maged M. wrote:
> 
> > Does anyone knows if normal ROR validation could be done inside of  
> > Radiant extensions. I’ve tried regular ROR validation methods like  
> > validates\_presence\_of inside of my model but it does’t work. Does  
> > anyone  
> > have an idea on how to do that.
> 
> Validation is a feature of ActiveRecord so it should work flawlessly  
> in a Radiant extension. Do you need to restart your mongrels? Do you  
> have a parse error in your models?
> 
> –  
> John L.  
> [http://wiseheartdesign.com](http://wiseheartdesign.com)

It didn’t give me any errors in the console and I restarted the server

That’s what the console looks like preparing my submit

\*\*

Processing AutoController#auto (for 127.0.0.1 at 2007-12-22 13:58:21)  
[POST]  
Session ID: a0297b2553d2c1ab1c7b036fcb6782af  
Parameters: {“commit”=\>“Next”, “action”=\>“auto”,  
“contact”=\>{“address1”=\>“”, “city”=\>“”, “work\_phone”=\>“”,  
“license\_state”=\>“”, “address2”=\>“”, “zip”=\>“”,  
“birth\_date(1i)”=\>“2007”, “birth\_date(2i)”=\>“12”,  
“birth\_date(3i)”=\>“22”, “country”=\>“USA”, “cell\_phone”=\>“”,  
“marital\_status”=\>“Single”, “home\_phone”=\>“”, “middle\_initial”=\>“E”,  
“first\_name”=\>“”, “last\_name”=\>“Makled”, “social\_number”=\>“”,  
“email”=\>“”, “driver\_license”=\>“”, “state”=\>“”}, “controller”=\>“auto”}

\*\*  
and my model  
\*\*  
class Contact \< ActiveRecord::Base

validates\_presence\_of :first\_name  
\*\*

any help will be appreciated.

Thanks  
Maged M.

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 22, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/4 "2007-12-22T22:06:47Z")

</div>

On Dec 22, 2007, at 2:08 PM, Maged M. wrote:

> “contact”=\>{“address1”=\>“”, “city”=\>“”, “work\_phone”=\>“”,  
> “license\_state”=\>“”, “address2”=\>“”, “zip”=\>“”,  
> “birth\_date(1i)”=\>“2007”, “birth\_date(2i)”=\>“12”,  
> “birth\_date(3i)”=\>“22”, “country”=\>“USA”, “cell\_phone”=\>“”,  
> “marital\_status”=\>“Single”, “home\_phone”=\>“”, “middle\_initial”=\>“E”,  
> “first\_name”=\>“”, “last\_name”=\>“Makled”, “social\_number”=\>“”,  
> “email”=\>“”, “driver\_license”=\>“”, “state”=\>“”}, “controller”=\>“auto”}

What does the code look like in your controller for saving the model?

Can you try this with script/console and see if you get an error:

> > c = Contact.new  
> > c.save!

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 22, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/5 "2007-12-22T22:15:32Z")

</div>

John W. Long wrote:

> On Dec 22, 2007, at 2:08 PM, Maged M. wrote:
> 
> > “contact”=\>{“address1”=\>“”, “city”=\>“”, “work\_phone”=\>“”,  
> > “license\_state”=\>“”, “address2”=\>“”, “zip”=\>“”,  
> > “birth\_date(1i)”=\>“2007”, “birth\_date(2i)”=\>“12”,  
> > “birth\_date(3i)”=\>“22”, “country”=\>“USA”, “cell\_phone”=\>“”,  
> > “marital\_status”=\>“Single”, “home\_phone”=\>“”, “middle\_initial”=\>“E”,  
> > “first\_name”=\>“”, “last\_name”=\>“Makled”, “social\_number”=\>“”,  
> > “email”=\>“”, “driver\_license”=\>“”, “state”=\>“”}, “controller”=\>“auto”}
> 
> What does the code look like in your controller for saving the model?
> 
> Can you try this with script/console and see if you get an error:
> 
> > > c = Contact.new  
> > > c.save!
> 
> –  
> John L.  
> [http://wiseheartdesign.com](http://wiseheartdesign.com)

When I tried with script/console here is what I got

* * *

Loading development environment.

> > c = Contact.new  
> > =\> #\<Contact:0xb79d4a74 @attributes={“city”=\>nil, “address1”=\>nil,  
> > “work\_phone”=\>nil, “license\_state”=\>nil, “zip”=\>nil, “address2”=\>nil,  
> > “country”=\>nil, “cell\_phone”=\>nil, “marital\_status”=\>nil,  
> > “birth\_date”=\>nil, “home\_phone”=\>nil, “middle\_initial”=\>nil,  
> > “first\_name”=\>nil, “social\_number”=\>nil, “last\_name”=\>nil, “email”=\>nil,  
> > “driver\_license”=\>nil, “state”=\>nil}, @new\_record=true\>  
> > c.save  
> > =\> false

* * *

and that’s what my action looks like  
\*\*  
def myAction  
radiant\_render :layout=\> “/sub/”  
if session[:contact].nil?  
contact=Contact.new(params[:contact])

```
    session[:contact]=contact

    contact.save
  end

```

end  
\*\*

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 22, 2007, 11:49pm UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/6 "2007-12-22T23:49:11Z")

</div>

Andrew O’Brien wrote:

> ActiveRecord::Base#save returns false on validation errors. It’s  
> ActiveRecord::Base#save! that throws an error. Try it with the  
> exclamation point.
> 
> -Andrew

Sorry could you eliborate more. Where to put the exclamation point.

Thanks  
-Maged

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 23, 2007, 12:22am UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/7 "2007-12-23T00:22:14Z")

</div>

Maged M. wrote:

> Andrew O’Brien wrote:
> 
> > ActiveRecord::Base#save returns false on validation errors. It’s  
> > ActiveRecord::Base#save! that throws an error. Try it with the  
> > exclamation point.
> > 
> > -Andrew
> 
> Sorry could you eliborate more. Where to put the exclamation point.
> 
> Thanks  
> -Maged

Got it, forgot the ! but here is my console when I ran it

Loading development environment.

> > c= Contact.new  
> > =\> #\<Contact:0xb79d9060 @attributes={“city”=\>nil, “address1”=\>nil,  
> > “work\_phone”=\>nil, “license\_state”=\>nil, “zip”=\>nil, “address2”=\>nil,  
> > “country”=\>nil, “cell\_phone”=\>nil, “marital\_status”=\>nil,  
> > “birth\_date”=\>nil, “home\_phone”=\>nil, “middle\_initial”=\>nil,  
> > “first\_name”=\>nil, “social\_number”=\>nil, “last\_name”=\>nil, “email”=\>nil,  
> > “driver\_license”=\>nil, “state”=\>nil}, @new\_record=true\>
> 
> > c.save!  
> > ActiveRecord::RecordInvalid: Validation failed: First name can’t be  
> > blank  
> > from  
> > ./script/…/config/…/config/…/vendor/radiant/vendor/rails/activerecord/lib/active\_record/validations.rb:764:in  
> > `save_without_transactions!' from ./script/../config/../config/../vendor/radiant/vendor/rails/activerecord/lib/active_record/transactions.rb:133:in`save!’  
> > from  
> > ./script/…/config/…/config/…/vendor/radiant/vendor/rails/activerecord/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:59:in  
> > `transaction' from ./script/../config/../config/../vendor/radiant/vendor/rails/activerecord/lib/active_record/transactions.rb:95:in`transaction’  
> > from  
> > ./script/…/config/…/config/…/vendor/radiant/vendor/rails/activerecord/lib/active\_record/transactions.rb:121:in  
> > `transaction' from ./script/../config/../config/../vendor/radiant/vendor/rails/activerecord/lib/active_record/transactions.rb:133:in`save!’  
> > from (irb):2

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 22, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/8 "2007-12-22T23:42:46Z")

</div>

ActiveRecord::Base#save returns false on validation errors. It’s  
ActiveRecord::Base#save! that throws an error. Try it with the  
exclamation point.

-Andrew

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 23, 2007, 12:25am UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/9 "2007-12-23T00:25:57Z")

</div>

Maged M. wrote:

> Maged M. wrote:
> 
> > Andrew O’Brien wrote:
> > 
> > > ActiveRecord::Base#save returns false on validation errors. It’s  
> > > ActiveRecord::Base#save! that throws an error. Try it with the  
> > > exclamation point.
> > > 
> > > -Andrew
> > 
> > Sorry could you eliborate more. Where to put the exclamation point.
> > 
> > Thanks  
> > -Maged
> 
> Got it, forgot the ! but here is my console when I ran it
> 
> Loading development environment.
> 
> > > c= Contact.new  
> > > =\> #\<Contact:0xb79d9060 @attributes={“city”=\>nil, “address1”=\>nil,  
> > > “work\_phone”=\>nil, “license\_state”=\>nil, “zip”=\>nil, “address2”=\>nil,  
> > > “country”=\>nil, “cell\_phone”=\>nil, “marital\_status”=\>nil,  
> > > “birth\_date”=\>nil, “home\_phone”=\>nil, “middle\_initial”=\>nil,  
> > > “first\_name”=\>nil, “social\_number”=\>nil, “last\_name”=\>nil, “email”=\>nil,  
> > > “driver\_license”=\>nil, “state”=\>nil}, @new\_record=true\>
> > 
> > > c.save!  
> > > ActiveRecord::RecordInvalid: Validation failed: First name can’t be  
> > > blank  
> > > from  
> > > ./script/…/config/…/config/…/vendor/radiant/vendor/rails/activerecord/lib/active\_record/validations.rb:764:in  
> > > `save_without_transactions!' from ./script/../config/../config/../vendor/radiant/vendor/rails/activerecord/lib/active_record/transactions.rb:133:in`save!’  
> > > from  
> > > ./script/…/config/…/config/…/vendor/radiant/vendor/rails/activerecord/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:59:in  
> > > `transaction' from ./script/../config/../config/../vendor/radiant/vendor/rails/activerecord/lib/active_record/transactions.rb:95:in`transaction’  
> > > from  
> > > ./script/…/config/…/config/…/vendor/radiant/vendor/rails/activerecord/lib/active\_record/transactions.rb:121:in  
> > > `transaction' from ./script/../config/../config/../vendor/radiant/vendor/rails/activerecord/lib/active_record/transactions.rb:133:in`save!’  
> > > from (irb):2

So it seems that it is doing validation, so why I’m not getting the  
regualr ROR Validation box on my page and it is going to the next page  
any ways?  
Any ideas

Thanks  
-Maged

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 23, 2007, 3:23am UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/10 "2007-12-23T03:23:54Z")

</div>

On Dec 22, 2007, at 6:25 PM, Maged M. wrote:

> So it seems that it is doing validation, so why I’m not getting the  
> regualr ROR Validation box on my page and it is going to the next  
> page  
> any ways?

Because the error\_messages\_for(‘contact’) helper call is not in your  
view:

[http://api.rubyonrails.com/classes/ActionView/Helpers/ActiveRecordHelper.html#M001005](http://api.rubyonrails.com/classes/ActionView/Helpers/ActiveRecordHelper.html#M001005)

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 25, 2007, 12:40am UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/11 "2007-12-25T00:40:07Z")

</div>

John W. Long wrote:

> On Dec 22, 2007, at 6:25 PM, Maged M. wrote:
> 
> > So it seems that it is doing validation, so why I’m not getting the  
> > regualr ROR Validation box on my page and it is going to the next  
> > page  
> > any ways?
> 
> Because the error\_messages\_for(‘contact’) helper call is not in your  
> view:
> 
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/ActiveRecordHelper.html#M001005)
> 
> –  
> John L.  
> [http://wiseheartdesign.com](http://wiseheartdesign.com)

I tried that but still didn’t work. Here is the question,  
even without error\_messages\_for , it is not supposed to go to the next  
page if I have validates\_presence\_of :first\_name in my model, right?

the weired thing is, it didn’t save anything to the DB but still went to  
the next page. here is how the top part of my view looks now.

\*\*  
\<%= error\_messages\_for ‘contact’ -%\>  
\<% form\_for :contact, :url =\> {:action =\> :auto, :id =\> @contact} do  
|form| %\>

* * *

Thanks

Maged M.

---

<div class="post-metadata">

**Author:** ![maged](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@maged](https://www.ruby-forum.com/u/maged)\
**Post date:** [December 25, 2007, 2:55am UTC](https://www.ruby-forum.com/t/validation-in-radiant/123930/12 "2007-12-25T02:55:43Z")

</div>

On Dec 24, 2007, at 6:40 PM, Maged M. wrote:

> I tried that but still didn’t work. Here is the question,  
> even without error\_messages\_for , it is not supposed to go to the next  
> page if I have validates\_presence\_of :first\_name in my model, right?

It really depends on how you wrote the action in your controller.  
Something like this might work:

def create  
if request.post?  
contact = Contact.new(params[:contact])  
if contact.save  
flash[:notice] = “Contact saved successfully.”  
redirect\_to show\_url(:id =\> contact)  
else  
flash[:error] = “Validation errors occurred while processing this  
form. Please take a moment to review the form and correct any input  
errors before continuing.”  
end  
end  
end

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)
