# :failure and :success in form\_remote\_tag

**URL:** <https://www.ruby-forum.com/t/failure-and-success-in-form-remote-tag/49829>\
**Category:** Rails\
**Created:** [December 9, 2005, 1:10am UTC](https://www.ruby-forum.com/t/failure-and-success-in-form-remote-tag/49829 "2005-12-09T01:10:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![a.filipowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@a.filipowski](https://www.ruby-forum.com/u/a.filipowski)\
**Post date:** [December 9, 2005, 1:10am UTC](https://www.ruby-forum.com/t/failure-and-success-in-form-remote-tag/49829/1 "2005-12-09T01:10:38Z")

</div>

I have a form that unfortunatly is not associated with a model as it  
is created dynamically based on selections from a user. I have a  
validation action called validate\_the\_form to make sure that fields  
the user signifies as required are acctually required. On submit I  
run the validation and redirect to the the form if there are any  
errors displaying the errors on top. Really I want to mimic the way  
that validate works in a model where there are errors on top as well  
as highlighting the fields that are missing the info that is needed  
but I also want to keep the values that are correct selected and  
filled out.

I was taking a look at the options for form\_remote\_tag and figured  
that I could submit the form via ajax and have set up the form remote  
tag like this:

\<%= form\_remote\_tag(:url =\> { :action =\> ‘validate\_the\_form’},  
:update =\> { :success =\> “form\_area”, :failure  
=\> “error\_area” }) %\>

my question is I am not sure where to go from here. i can’t figure  
out how to define what is a failure. Am I going down the right track?  
or is there another way to do this? I also looked in my copy of Agile  
Web Dev and they have an example but it does not keep values that are  
selected and or filled out in the form if there is a failure.

Thanks for all the help

Andrew

---

<div class="post-metadata">

**Author:** ![a.filipowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@a.filipowski](https://www.ruby-forum.com/u/a.filipowski)\
**Post date:** [December 9, 2005, 1:26am UTC](https://www.ruby-forum.com/t/failure-and-success-in-form-remote-tag/49829/2 "2005-12-09T01:26:55Z")

</div>

On a side note I did look at the example on the wiki as well as this  
one “Validations for non-ActiveRecord Model Objects” but I don’t  
think that they will work for me as I don’t have a model for this  
form. The form is built dynamically based on several other models but  
because the options that I need to validate are not constant (for  
example a set of radio buttons could require that there is a choice  
or not require one at all) based on what the user defines. Since  
these fields are completely unknown I am not sure where to go.

Andrew

---

<div class="post-metadata">

**Author:** ![a.filipowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@a.filipowski](https://www.ruby-forum.com/u/a.filipowski)\
**Post date:** [December 9, 2005, 3:28am UTC](https://www.ruby-forum.com/t/failure-and-success-in-form-remote-tag/49829/3 "2005-12-09T03:28:44Z")

</div>

> 

Here is a very nice way of still being able to use validations  
without the need for a database model. It just fakes rails out into  
thinking the model is an AR model and you just cant .save it , you  
call .valid? on it to check the validations:

class Contact \< ActiveRecord::Base  
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

column :name, :string  
column :city, :string  
column :state, :string  
column :phone, :string  
column :email\_address, :string

validates\_presence\_of :name, :email\_address  
end

Cheers-

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![a.filipowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@a.filipowski](https://www.ruby-forum.com/u/a.filipowski)\
**Post date:** [December 9, 2005, 3:44am UTC](https://www.ruby-forum.com/t/failure-and-success-in-form-remote-tag/49829/4 "2005-12-09T03:44:57Z")

</div>

On Dec 8, 2005, at 6:24 PM, Ezra Z. wrote:

> ```
> columns << ActiveRecord::ConnectionAdapters::Column.new 
> 
> ```
> 
> end

Oh and I forgot to credit technoweenie for writing it 😉

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![a.filipowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@a.filipowski](https://www.ruby-forum.com/u/a.filipowski)\
**Post date:** [December 9, 2005, 2:42pm UTC](https://www.ruby-forum.com/t/failure-and-success-in-form-remote-tag/49829/5 "2005-12-09T14:42:32Z")

</div>

I looked at that but not sure that it will work in my situation. as I  
don’t know what will be required or not ahead of time. Since I don’t  
know what will be required I can’t define the validate\_presence\_of  
the fields. Also I don’t know what the field names are ahead of time  
either. They are generated on the fly when the page is rendered. Here  
is a look at my validate method that I have written. I think I am on  
the right track writing my own action just not sure how to repopulate  
the fields if there is an error.

def validate\_the\_form  
@errors = []  
@ theform = Form.find(params[: theformid])  
# loop through all the fields to validate the responses given match  
the requirements defined  
# by the form author  
@form.questions.each do |@question|  
# Check to see if the question is required  
if @question.required == 1  
# we need to do something special to validate if the checkbox  
type is required  
# here we check to see if it is a checkbox  
if @question.field\_type == ‘checkbox’  
# if it is a check box we loop thru the choices to check if they  
are checked  
counter = 0  
@question.choices.each do |@choice|  
if params["#{@choice.id}question\_#{@question.id}"] != nil  
counter = counter + 1  
end  
end  
# if no check boxes are checked we kick out an error  
if counter == 0  
@errors \<\< “#{@question.question\_text}”  
end  
# if it is not a checkbox we make sure that there is not a null  
value  
else  
if params[“question\_#{@question.id}”] == nil or params  
[“question\_#{@question.id}”] == ‘’  
@errors \<\< “#{@question.question\_text}”  
end  
end  
end  
# we next check our checkbox type to make sure that the minimum and  
# maximum values defined by the offer are adhered to  
if @question.field\_type == ‘checkbox’  
# make sure that maximum responses are adhered to  
if @question.max\_responses != nil  
if checked \> @question.max\_responses  
@errors \<\< “#{@question.question\_text} to many responses”  
end  
end  
# make sure that minimum responses are adhered to  
if @question.min\_responses != nil  
if checked \< @question.min\_responses  
@errors \<\< “#{@question.question\_text} to few responses”  
end  
end  
end  
end  
# if we have no errors submit the form other wise we kick back to  
the form  
if @errors.size == 0  
submit\_the\_form  
else  
render :action =\> ‘take’, :id =\> @theform.id, :errors =\> @errors  
end  
end

my view right now has allot of logic in it to build the form. I think  
that I should be using helpers for it but it works right now. This  
app is in scaffold stage right now. I just want to get it working and  
than will go back and do some code cleanup during the the time that I  
make the views pretty.

here is just a piece of the view:

\<% if params[:errors] != nil %\> \<% 0.upto(params[:errors].size - 1) do |x| %\> The Question: \<%= params[:errors][x] %\>  
 Is Required  
  
 \<% end %\> \<% end %\>

\<%= flash[:notice] %\>

\<%= @theform.name %\>
\<%= form\_remote\_tag(:url =\> { :action =\> 'validate\_the\_form'}, :update =\> { :success =\> "form\_area", :failure =\> "error\_area" }) %\> \<%= hidden\_field\_tag(: theformid, @ theform.id) %\> \<% @questions.each do |@question| %\> \<%= @question.question\_text %\>  
 \<% if @question.field\_type == 'radio' %\> \<% @question.choices.each do |@choice| %\> \<% questionid = "question\_#{@question.id}" %\> \<%= radio\_button\_tag("question\_#{@question.id}", @choice.choice\_text) %\>\<%= h(@choice.choice\_text) %\>  
 \<% end %\>

I than go through a bunch of more elsifs to create the different  
fields (checkbox, textfield, textarea and so on)

\<%= submit\_tag ‘Submit’ %\>  
\<%= end\_form\_tag %\>

As I said I think that I am heading in the right direction I am just  
not sure how to kick back a failure from the validate method to the  
form\_remote\_tag

Andrew
