Validation in Radiant

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.

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

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

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.

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

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

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
**

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

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:insave!’
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:intransaction’
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:insave!’
from (irb):2

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

-Andrew

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:insave!’
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:intransaction’
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:insave!’
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

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


John L.
http://wiseheartdesign.com

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


John L.
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.

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