Forum: Ruby on Rails Problem with disable_with

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
907787feaa86eb2c256916e4ff21a911?d=identicon&s=25 Walter Lockhart (Guest)
on 2009-01-08 19:20
(Received via mailing list)
Hi Group,

I have a registration.html.erb page with the following JavaScript at
the top which I use to validate form fields:

<script type="text/javascript">
<!--
function validate(){
if ((document.myForm.email_projectname.value=="")||
(document.myForm.email_projecttype_id.selectedIndex<1)||
(document.myForm.email_region_id.selectedIndex<1))
    {
        alert ("You must fill in all of the required fields!")
        return false
    }
else
    return true;
}
//-->
</script>

further down the page I have the form:

<% form_tag({:action => 'sendregistration'}, {:name =>
"myForm", :onSubmit => "return validate()"}) do -%>

<tr><td style="width:50%;">Project Name:<strong>&nbsp;*</strong></
td><td><%= text_field 'email', 'projectname', :size => 30 %></td></tr>
<tr><td style="width:50%;">Project Type:<strong>&nbsp;*</strong></
td><td><%= collection_select 'email', 'projecttype_id',
@projecttypes, :id, :name, { :include_blank => true } %></td></tr>
<tr><td style="width:50%;">Region:<strong>&nbsp;*</strong></td><td><%=
collection_select 'email', 'region_id', @regions, :id, :name,
{ :include_blank => true } %></td></tr>

etc.

<%= submit_tag "Send", :class => "submit", :id =>
"mysubmit", :disable_with => "Please wait..." %>

<% end -%>

When I click 'Send' without the required fields the 'Send' button is
disabled and the text is replaced with 'Please wait...' which is as I
expected.  It also performs the 'validate' JavaScript function and
discovers that the required fields are not provided and displays a
popup window with the text 'You must fill in all of the required
fields!'

When I click 'Send' with the required fields the 'Send' button is
disabled and the text is replaced with 'Please wait...' which again is
as I expected.  However, the form does not Submit / Post, it just
hangs.

What am I doing wrong?  Would someone please help.

Thanks.

Regards

Walter
B7730b92cc13a77fa50752ce81e10468?d=identicon&s=25 Klaus Paiva (Guest)
on 2009-01-08 20:26
(Received via mailing list)
You can always return false and inside the else block, add this
javascript that will submit the form:
document.forms["myForm"].submit();
907787feaa86eb2c256916e4ff21a911?d=identicon&s=25 Walter Lockhart (Guest)
on 2009-01-08 21:30
(Received via mailing list)
Hi Klaus,

thanks for your response.  I tried this:

<script type="text/javascript">
<!--
function validate(){
if ((document.myForm.email_projectname.value=="")||
(document.myForm.email_projecttype_id.selectedIndex<1)||
(document.myForm.email_region_id.selectedIndex<1)
    ||(document.myForm.email_message_body.value==""))
    {
        alert ("You must fill in all of the required fields!")
        return false
    }
else
{
document.forms["myForm"].submit();
return false;
}
}
//-->
</script>

which I hope is what you were proposing in your response, but it still
doesn't Post.  It just hangs.

Regards

Walter
907787feaa86eb2c256916e4ff21a911?d=identicon&s=25 Walter Lockhart (Guest)
on 2009-01-08 21:39
(Received via mailing list)
Hi Klaus,

now I'm really confused.  When I remove the validation and use return
true:

<script type="text/javascript">
<!--
function validate(){

document.forms["myForm"].submit();
return true;

}
//-->
</script>

It posts, no problem.

Any ideas?

Regards

Walter
907787feaa86eb2c256916e4ff21a911?d=identicon&s=25 Walter Lockhart (Guest)
on 2009-01-08 22:02
(Received via mailing list)
Hi Klaus,

thanks for your help. The following code now works:

<script type="text/javascript">
<!--
function validate(){
if ((document.myForm.email_projectname.value=="")||
(document.myForm.email_projecttype_id.selectedIndex<1)||
(document.myForm.email_region_id.selectedIndex<1))
    {
        alert ("You must fill in all of the required fields!");
        return false;
    }
else
    {
        document.forms["myForm"].submit();
        return true;
    }
}
//-->
</script>

Thanks again.

Regards

Walter
This topic is locked and can not be replied to.