Geo
Geo

Reputation: 96817

jQuery and Rails 3, what am I doing wrong?

I have a form which contains among others:

<form id="new_basic_ad" class="new_basic_ad" method="post" enctype="multipart/form-data" action="/basic_ads" accept-charset="UTF-8">
  <select name="category_load_name" id="category_loader">
     <option value="">Choose one</option>
     <option value="1">First</option>
     <option value="2">Second</option>
  </select>
</form>

And I want to add client side validations. I tried adding this to my application.js:

$.validator.addMethod("valueNotEquals", function(value, element, arg){
        return arg != value;
    }, "Value must not equal arg.");


    $("#new_basic_ad").validate({
        rules: {
            category_loader: {
                valueNotEquals: "Choose one"
            }
        },
        submitHandler:function(form) {
            form.submit();
        }
    });

I want to make it so that the form cannot be submitted if the select is on the default ( Choose one ) value. However, my form is submitting ( without errors, apparently ), and I don't understand why it's not applying the validation. Should the validation be done on a live("submit",function(...)) ?

Upvotes: 0

Views: 79

Answers (1)

Geo
Geo

Reputation: 96817

I was using the id of the element, instead of it's name. The code becomes now:

$("#new_basic_ad").validate({
        rules: {
            category_load_name: {
                valueNotEquals: "Choose one"
            }
        },

Upvotes: 1

Related Questions