JQuery Tools validator Did didn't work over dynamic fields

804 Views Asked by At
function getGroup(el){
    return $(el).parents('.field');
}

function validateGroup(group){
    var count = 0;
    $(group).find("input[type!='hidden'], select").each(function(){
        if( $(this).val() )
            count++;
    });
    if($(group).find("input[type!='hidden'], select").length == count || count == 0)
        return true;
    else
        return false;
}
    $.tools.validator.fn(
    $("#addressInfo .field select, #addressInfo .field input[type!='hidden']"),
    "Please Complete this mandatory field",
    function(el, v){
        if( !testOptionalFieldsValidation(el) && ($(el).val().length == 0 ) )
            return false;
        else
            return true;
    }
    );
    function testOptionalFieldsValidation(el){
    group = getGroup(el);
    return validateGroup(group);
}

"

Now the above specified code am using for fields validation. The scenario is something like that if a user enter value of single field he suppose to enter all of them other wise validator point out those field to be fill. The above specified code was working according to our requirement until now but now we also have given an optional loop able Address Info section where he can enter more details by click a button which add again clone of fields like street(input type) city (input type) State (select type) Country (select type).. so in total default fields are 6 but after clicking add more address info button it should validate all 12 fields instead of just 6 of them and show error messages respectively. According to best of my knowledge and skills its a bug in JQUERY tools that it didn't update its validation list or i missing some of its function or handler.

1

There are 1 best solutions below

0
On
$('form').submit(function(){
    $('form').validator();
    $.tools.validator.fn(
        $("#addressInfo .field select, #addressInfo .field input[type!='hidden']"),
        "Please Complete this mandatory field",
        function(el, v){
            if( !testOptionalFieldsValidation(el) && ($(el).val().length == 0 ) )
                return false;
            else
                return true;
        }
        );
    function testOptionalFieldsValidation(el){
        group = getGroup(el);
        return validateGroup(group);
    }

});

The fix to my code is that i need to initialize validator by calling constructor on submit event even initialized my custom validation function.