Defining custom validation methods

Support MB Frontend Submission Defining custom validation methods

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #9185
    calpaqcalpaq
    Participant

    Dear Support,
    We just purchased metabox development version. It is very useful. In one of our website we need to use the metabox extension MB Frontend Submission & created a form. Now we need to validate the fields and your default validation rules came to help mostly, but we need to create a custom validation rule/method and had some experience creating them with addMethod function when I was using jquery validation plugin you mentioned, but it gives me error when I am trying to define a new rule with addMethod. Can you please help me in this?

    Note: the error message is Uncaught TypeError: Cannot read property 'addMethod' of undefined at HTMLDocument.<anonymous> when I am writing the code as follows

    $.validator.addMethod('checkXiiAggregate',function(element, value){
            if (check_all_XII_fields_empty()) 
                if ( element  == "") return true; 
            if ($("#cmbclassxiiyearofpassing").val() != $("#lte_current_year").val()) {
                if (element == ""){
                    return false;
                }
            }
            return true;
        }, "Please provide aggregate marks.")

    Thanks in advance

    #9198
    Anh TranAnh Tran
    Keymaster

    Hello,

    I've just tested and the custom validation rule works. I think the problem might be the way you enqueue your script (that contains custom rule). Somehow, it's enqueued before jQuery or jQuery validation script is output.

    In my test, I use wp_footer hook with priority 9999 to enqueue the JS, so it's always after jQuery and jQuery validation. Please try and let me know how it goes.

    #9201
    calpaqcalpaq
    Participant

    Hi,
    Thanks for your reply, I figured out the problem, I was testing a form in the admin and the jquery.validation.js is not getting enqueued by the plugin so I have done it through my plugin.

    #9225
    Anh TranAnh Tran
    Keymaster

    Glad that you have resolved it.

    The jQuery validation plugin is enqueued only when there's any validation rule. And because it's outputted in the admin footer, your script need to be outputted after that. I'd suggest using hook admin_print_footer_scripts with high priority to output it. Do not use admin_footer as it fires before admin_print_footer_scripts, which is used to output enqueued JS.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Defining custom validation methods’ is closed to new replies.