7

I have a form that can be used both remotely and normally.

= form_for @comment, html: { class: 'comment-form' }, remote: request.xhr? do |f|
  = f.text_area :body
  = f.submit

I want the form to submit only if the body textarea has content:

  $(document).on 'submit', '.comment-form', (e) ->
    text = $(this).find('#comment_body').val()
    false if text.length < 1

That code works when the form is not Ajax. But when it's remote, it fails and the form still submits. Why? I also tried:

if text.length < 1
  e.preventDefault()
  false

I'm using Rails 4 with Turbolinks.

Update: I tried binding the ajax:beforeSend event, it still submits:

  $(document).on 'page:load', '.comment-form', ->
    $(this).bind 'ajax:beforeSend', ->
      alert "hi"

I see no alert...

user2630970
  • 153
  • 2
  • 11

4 Answers4

5

I'm using Rails 5.0.0.1 and as described at jquery-ujs documentation in Stoppable events section you only need to return a false value in the ajax:beforeSend event to stop the request and prevent the form from being send.

So this works perfectly for me:

$(document).on("ajax:beforeSend", "form#messages_form", function (e, data, status, xhr) {
    var len = $.trim($('#message_body').val()).length;

    if (len > 3) {
        console.log("Send form!");
        return true;
    } else {
        console.log("Stopping request...");
        return false;
    }
});

I hope it could be useful for somebody else.

alexventuraio
  • 8,126
  • 2
  • 30
  • 35
  • Works. But what is the thing that is causing the form to be submitted twice in the first place? – W.M. Sep 27 '17 at 19:32
  • 1
    I don't know which is your case exactly but in this case the question was how to prevent the form from being sent if something special happens. I had a similar case _the form was being *submitted twice*_ and it was because in a `link_to` I used `remote: true` and at the same time I had and ajax request triggered with one of the `classes` my link was using. – alexventuraio Sep 28 '17 at 16:08
  • If the form is already in the DOM upon page load, `$("form#messages_form").on("ajax:beforeSend", function ...)` will suffice. – Dennis Hackethal Aug 16 '23 at 16:55
4

You could potentially implement a handler for the ajax:beforeSend event to prevent your form from being submitted. It looks like the submit stops if your event handler returns false.

Wiki: https://github.com/rails/jquery-ujs/wiki/ajax

Example implementation: Stop $.ajax on beforeSend

Community
  • 1
  • 1
lmjohns3
  • 7,422
  • 5
  • 36
  • 56
0

Maybe I didn't get your question, but shouldn't it be enough to add a

:required => true

to your text_area ?

Miotsu
  • 1,776
  • 18
  • 30
  • `required: true` adds an HTML5 attribute that doesn't work on half the browsers. It's not dependable. At least not at this time. – user2630970 Aug 28 '13 at 17:05
0

This looks like a jquery_ujs issue. Try changing the event to:

 $('.comment-form').on 'submit', (e) ->

I'm facing the same issue and only this solved the problem.

Hugo Dias
  • 169
  • 3
  • 14