Uncaught TypeError: Can not read property 'validDocument' of undefined

1

I'm trying to create a validation function inside ValidationEngine, however I'm getting this error:

Uncaught TypeError: Can not read property 'validDocument' of undefined

E I can not identify the problem. I have another site using the same function, and I can not see where I'm going wrong.

My code:

<!DOCTYPE html>
<html class="no-js" lang="pt-br">
    <head>
        <title>Teste</title>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script><scriptsrc="http://code.jquery.com/jquery-migrate-1.4.0.js"></script>
        <script src="/site/js/script/validationEngine/jquery.validationEngine.js"></script>
        <script src="/site/js/script/validationEngine/jquery.validationEngine-pt-BR.js"></script>
    </head>
    <body>
        <script>
            $(function(){
                $(".validar").validationEngine("attach");
                function validaDocumento(field, rules, i, options){ return true; }
                $(this).submit(function(e){ e.preventDefault(); });
            });
        </script>
        <form method="POST" class="validar formConsulta">
            <label>CPF/CNPJ:</label>
            <input type="text" class="validate[funcCall[validaDocumento]]" data-errormessage="Documento invalido!" />
            <input type="submit" value="Consultar">
        </form>
    </body>
</html>

Link to access code: link

I have tried to change the order of Javascript (put on top, on the head, after all, reverse the functions) and always gives the same error.

What can I do to remedy the problem?

    
asked by anonymous 25.10.2017 / 19:23

1 answer

5

The problem is here

$(function(){
    $(".validar").validationEngine("attach");
        function validaDocumento(field, rules, i, options){ return true; }
            $(this).submit(function(e){ e.preventDefault(); });
    });
});

The code above will only be executed after the page loads, you are importing the Scripts , the page is being rendered, consequently validate[funcCall[validaDocumento]] is running after import and before $(function(){ , because script will only run when the page is loaded.

You should put the validaDocumento method out.

$(function(){
    $(this).submit(function(e){ e.preventDefault(); });
    $(".validar").validationEngine("attach");
});
function validaDocumento(field, rules, i, options){ return true; }

See working:

<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script><scriptsrc="https://code.jquery.com/jquery-migrate-1.4.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/jquery.validationEngine.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/languages/jquery.validationEngine-pt_BR.min.js"></script>
<script>
$(function(){
    $(this).submit(function(e){ e.preventDefault(); });
    $(".validar").validationEngine("attach");
});
    function validaDocumento(field, rules, i, options){ return true; }
</script>
<form method="POST" class="validar formConsulta">
    <label>CPF/CNPJ:</label>
    <input type="text" class="validate[funcCall[validaDocumento]]" data-errormessage="Documento invalido!" />
    <input type="submit" value="Consultar">
</form>
    
25.10.2017 / 19:32