Rails AJAX pre-submit form validation

It’s been awhile since a post, but I was on vacation, and then there is the post vacation week of recovery. I don’t have anything big to post, but I figured I’d better post something otherwise I’ll just give up completely on this blog and I don’t want to do that.

So a brief little problem I’ve been working on today is how to do some pre-submit client side validation on a form element in rails if I’m using the remote_form_tag to create my tag. I found a great little solution that was sitting right in front of me the whole time, but it took this article for me to see it. Basically you just use the :before parameter to call your validation script and then just return false if it fails, how great is that. Just another rails solution that ends up being easy as long as you just look at the available tools the right way.

15 Responses to “Rails AJAX pre-submit form validation”

  1. Buffalo Says:

    I have been banging my head against this solution all morning, I just cant seem to get it to work.

    The following combination of the form_remote_tag, :before, and javascript causes the form to still submit and the action to execute.

    Mind to have a look and tell me if I am missing something obvious? I am new to ruby on rails , but relatively adept at javascript, I am hoping I am just doing something blatently wrong .. Thanks! Feel free to email me or I can check back here.

    # the form tag
    “ValidateForm()”,
    :url => { :action => “execute” },

    ) %>

    # the javascript
    function ValidateForm() {

    return false
    }

  2. Jeff Says:

    your code snippet is a little hard to read, just wanted to make sure you are calling :before => “ValidateForm()” and not just , “ValidateForm()”

  3. kellybluebook Says:

    good site!

  4. hillemania Says:

    Thanks Kellybluebook, I’m glad some people are reading it and enjoying it.

  5. kori Says:

    Hope you come back soon!! notem6715

  6. Venkat Says:

    Hi all, by reading the above comments, I thought that you all might have successfully implemented javascript validation before submitting form to a remote action. But when I tried this with my app, I was left unsuccessful.
    Below is the code I have.

     {:action => “does”}, :before => “validate_form()”, :update => “update_div” ) %>
    input type="text" name="name" id="name"
    div updates here
    

    In my does.rhtml, I am just displaying some text.

    this is my javascript code.

    function validate_form()
    {
    var name = document.getElementById(’name’).value;
    var errors = “”;
    if(name != “kishore”)
    {
    errors+= “invalid name”;
    }
    if(errors != “”)
    {
    alert(errors);
    return false;
    }
    }
    

    When I enter the correct data, the text is getting rendered successfuly but when data is entered incorrectly, the alert message gets displayed and the request is still getting processed and the div is getting updated with the text. Where am I missing the obvious thing? Any help appreciated. thanks in advance.
    Can mail the stuff if the code here is obscure.

    regards,
    venkat

  7. Floroskop Says:

    Hello!
    I think this try.

  8. name Says:

    Good day!,

  9. No_limits73 Says:

    Acceptance of their results relies on the process of obtain- ing the results as well as analysis of the results themselves. ,

  10. SouthWind41 Says:

    Short term employment for youth workers, or even job-creation measures that bring in workers without the proper skills to work with youth, can be counter-productive in terms of creating a successful environment for youth participation. ,

  11. PiterJankovich Says:

    My name is Piter Jankovich. oOnly want to tell, that your blog is really cool
    And want to ask you: is this blog your hobby?
    P.S. Sorry for my bad english

  12. Emerson Lackey Says:

    I have my own solution for AJAX validations that I think is quite clean and railsey. I actually put together a screencast on the subject – feel free to check it out at:

    http://www.emersonlackey.com/article/rails-ajax-validations-jquery

  13. lakshmanan Says:

    NOT WORKING…… the request is still getting submitted …

  14. vijaya Says:

    On using if (! validate_form()) {return false;} the request is still getting processed

  15. Johnd92 Says:

    I conceive this website has got some rattling superb info for everyone. Dealing with network executives is like being nibbled to death by ducks. by Eric Sevareid. kkcafdbcbdec

Leave a comment