Saurav Chakraborty
2008-Apr-22 05:34 UTC
problem in form validation of input using onSubmit
Hi, Im trying to check for uploading of files of only doc and ps types. For that matter I have used two javascript funtions and the form. Im trying to use onSubmit event handler along with form_for.But the problem is that, files of any types are getting uploaded. Can anybody point me out what am I doing wrong here ? -Thanks Saurav the javascript functions are <script> extArray = new Array(".doc",".ps"); function checkFile(file){ fileType = false; if (!file) return false; while (file.indexOf("\\") != -1) file = file.slice(file.indexOf("\\") + 1); ext = file.slice(file.indexOf(".")).toLowerCase(); for (var i = 0; i < extArray.length; i++) { if (extArray[i] == ext) { fileType = true; break; } } if (!fileType) { alert("Please upload files that end in types: " + (extArray.join(" ")) + "\nPlease select a new " + "file to upload and submit again."); file1.focus(); return false; } else return true; } function validate_form_fields(this) { var file1 = document.getElementById("this.file1"); if (checkFile(file1)) return true; else return false; } </script> the code for form is : <%= error_messages_for :project %> <tr> <td> <% form_for :project, :url => {:controller=>:project,:action => "create"},:html=>{:multipart => true, :name => "newproject_form", :onSubmit => "return validate_form_fields(this);"} do |f| -%> <!--form section--> <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0> <TR> <TD HEIGHT=15 COLSPAN=11></TD> <TD WIDTH=6 HEIGHT=15></TD> </TR> <TR> <TD width="89" HEIGHT="27">Project Name</TD> <TD HEIGHT="27" ><%= f.text_field "name" %></TD> </TR> <TR> <TD HEIGHT=25>Description</TD> <TD HEIGHT=25 align="left"><%= f.text_area "description", :cols => 40, :rows => 10 %></TD> </TR> <tr> Please upload only files that end in: .doc and .ps </tr> <TR> <TD HEIGHT=24>Upload Files </TD> <TD HEIGHT=24 ><%= file_field_tag :file1 %></TD> </TR> <TR> <TD height="28" > </TD> <TD><a href="javascript:document.newproject_form.submit()";><img src="/images/create.jpg" /></a></TD> <TD> </TD> <TD></TD> </TR> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Saurav Chakraborty
2008-Apr-22 07:42 UTC
Re: problem in form validation of input using onSubmit
Hi everybody, I have figured it out actually the problem was that the the values were not getting passed to the javascript method. Now its working. Now there is another issue i want to know instead of tag "Create" in <%= submit_tag ''Create'' %> can i display some image out there so that when i click on the image the form gets submitted. in html i know we can do something like this <a href="javascript:document.newproject_form.submit()";><img src="/images/create.jpg" /></a> Is there any equivalent in erb ?? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
image_submit_tag should do it. http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M001046 -Kyle On Apr 22, 2:42 am, Saurav Chakraborty <rails-mailing-l...@andreas- s.net> wrote:> Hi everybody, > I have figured it out actually the problem was that the the values were > not getting passed to the javascript method. Now its working. > > Now there is another issue i want to know > > instead of tag "Create" in <%= submit_tag ''Create'' %> can i display some > image out there so that when i click on the image the form gets > submitted. > > in html i know we can do something like this > > <a href="javascript:document.newproject_form.submit()";><img > src="/images/create.jpg" /></a> > > Is there any equivalent in erb ?? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Saurav Chakraborty
2008-Apr-23 02:44 UTC
Re: problem in form validation of input using onSubmit
Ya I got it yesterday ...n now its working fine...nyways thanks for the information -Saurav Kyle wrote:> image_submit_tag should do it. > > http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M001046 > > -Kyle > > On Apr 22, 2:42 am, Saurav Chakraborty <rails-mailing-l...@andreas--- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---