0

I have a nested form that uses jquery validate My question is , Can we validate/not validate entire form based on certain values?. For example I have a nested form with save / submit buttons. I want to validate only on Submit. My problem is that I have the class="required" on each field as the form is very complex and I build it on the fly. Please help.

    (function($){ 

$(document).ready(function() {

    $(".form").validate({
        debug:true,
        errorClass: "errormsg",
        ignore: ':hidden',
        meta: 'validate',
        submitHandler: function(form) {
            if ($("#stype").val() == "Submit") {

            }else{
                    form.submit();
            }
        }
    });
} );
})(jQuery);

My html looks somthing like...

<tr>
  <td width="55%">Name of Service</td>
  <td>
     <input class="required" id="application_contact_attributes__name" name="application[contact_attributes][name]" size="30" type="text" value="Demo Service 10" />
  </td>
</tr>
Aanu
  • 4,221
  • 4
  • 18
  • 15
  • 1
    Can you not just remove the `class="required"` from the fields that aren't required? – hughes Aug 18 '11 at 18:42
  • 2
    Nested forms are [not](http://www.w3.org/MarkUp/html3/forms.html) [valid](http://stackoverflow.com/questions/379610/can-you-nest-html-forms) [HTML](http://stackoverflow.com/questions/597596/how-do-you-overcome-the-html-form-nesting-limitation). – George Cummins Aug 18 '11 at 18:45
  • @hughes my requirement is I have to either validate all the fileds when I submit or not validate when I save. – Aanu Aug 18 '11 at 18:50
  • @George Cummins - Not a nested form. Its a form with rails model thats nested. That is 1 parent - multiple child, grand child. I will edit the post. – Aanu Aug 18 '11 at 18:51

1 Answers1

1

From the documentation

Can you use the onfocusout and onkeyup handlers and disable checking? I imagine these are the ones doing the validation "as you go."

$(".form").validate({
    debug:true,
    errorClass: "errormsg",
    ignore: ':hidden',
    meta: 'validate',
    onfocusout: false,
    onkeyup: false,
    onclick: false,
    submitHandler: function(form) {
        if ($("#stype").val() == "Submit") {

        }else{
                form.submit();
        }
    }
});
mrtsherman
  • 39,342
  • 23
  • 87
  • 111