How to store the variable returned from a closure function?
<p>Noticed the code of the <code>submitHandler</code> was being used in several places, and tried to refactor it out.</p>

<pre><code>var validator = $("#form_id").validate({
            errorClass: "ui-state-error",
            submitHandler: function(form) {
                jQuery(form).ajaxSubmit({
                    dataType: 'json',
                    success: function(entity) {
                        //dostuff
                    },
                    error: function(errors) {
                            validator.showErrors(errors);
                    }
                });
            }
        });
</code></pre>

<p>I took the code above and extracted the submithandler to a function like so;</p>

<pre><code>$("#generated_form_id").validate({
                errorClass: "ui-state-error",
                submitHandler: bindSubmitHandler(formId)
                }
            });

    function bindSubmitHandler(formId){
            return function(form) {
                    jQuery(form).ajaxSubmit({
                        dataType: 'json',
                        success: function(object) {
                            //do stuff
                            });
                        },
                        error: function(errors) {
                                validator.showErrors(errors); //throws an exception
                        }
                    });
                }
        }
</code></pre>

<p>This code throws an exception because the validator object is undefined. </p>

<p><em>My own reasoning</em> which may be false has been as follows: I can't store the variable, because the forms are being created dynamically. One page could create ten of these forms using the same <code>bindSubmitHandler</code> function. 
I thought about the solution of getting the validator via the form, but couldn't find any such method in the validator API. If there is such a method, I could write something like <code>$('#formid').getValidator().showErrors(errors);</code> </p>

<p><strong>Does anyone have an idea for a solution to this problem?</strong></p>
