Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery validation plugin, conditional remote call not working as expected
    text
    copied!<p>I am trying to setup a conditional remote call using the standard jquery validation plugin.</p> <p>The problem is when I pass in the name.val() in the validate method it's always the name that was set in the input text field when the page loads. It's never the name in the text box.</p> <p>I replaced name.val() with "Test" and then the script works flawlessly. There are no syntax errors or any errors in the console. <strong>I also removed the call to get_exists_rule(name.val()) and replaced it with a simple required: true and it's fine. I have this setup working on other forms with the same naming scheme, just no remote calls.</strong></p> <p>I also threw an alert msg in the get_exists_rule and no matter what it only gets executed once when the page loads so there might be some funny business going on.</p> <p>I've been stuck on this for close to 7 hours straight, I looked at every related SO post and everywhere I could think of and did not find a solution that works.</p> <p><strong>Here's the coffeescript:</strong></p> <pre><code> name = $("#category_name") get_exists_rule = (category_name) -&gt; if original_name is category_name required: true maxlength: 64 else remote: { url: "/dashboard/categories/exists/" + title_to_slug(category_name), type: "POST", async: false } category_form.validate errorElement: "span" rules: "category[name]": get_exists_rule(name.val()) </code></pre> <p><strong>Here's the javascript:</strong></p> <pre><code>var get_exists_rule, name; name = $("#category_name"); get_exists_rule = function(category_name) { if (original_name === category_name) { return { required: true, maxlength: 64 }; } else { return { remote: { url: "/dashboard/categories/exists/" + title_to_slug(category_name), type: "POST", async: false } }; } }; category_form.validate({ errorElement: "span", rules: { "category[name]": get_exists_rule(name.val()) } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload