简体   繁体   English

重用来自匿名函数的 jQuery 验证

[英]reuse jQuery validate from anonymous function

I am getting a typeError undefined is not a function error when trying to validate the visible fields.尝试验证可见字段时,我收到typeError undefined is not a function错误。

I defined the variable that would hold the jquery validator in the global scope, but executed .我定义了在全局范围内保存jquery 验证器的变量,但执行 . validate () from within an anonymous function closure which is dynamically generated by symfony/twig/JqueryValidationBundle. validate () 来自一个匿名函数闭包,它是由 symfony/twig/JqueryValidationBundle 动态生成的。

I did this because I wanted to use the generated code for a custom function that is contained in a separate js file.我这样做是因为我想将生成的代码用于包含在单独 js 文件中的自定义函数。

Is it possible to do this?是否有可能做到这一点? or is there another way to reuse the generated code?还是有另一种方法可以重用生成的代码?

This is the generated function:这是生成的函数:

<script>
var form, groups;
(function($) {
    form = $("form[name=\"scs_intakebundle_intake\"]");
    groups = {"Default": false,"firstPanel": false};
    form.find("*[name=\"scs_intakebundle_intake\x5Bsubmit\x5D\"]").click(function() {
        groups = {"Default": true,"firstPanel": true};
    });

    form.validate({
        rules: {
            "scs_intakebundle_intake\x5BfirstName\x5D": {
                "required": {depends: function() {return groups["firstPanel"]; }}
            },
            "scs_intakebundle_intake\x5BlastName\x5D": {
                "required": {depends: function() {return groups["firstPanel"]; }}
            }
        }});
})(jQuery);
</script>

This is the function from intake.js这是来自intake.js的函数

function nextPanel() {
    var result = true;
    $('input:visible').each(function(i,item){
        //form is the global var that .validate() was already run.
        //below is where form.element is undefined.
        result = form.element(item) && result;
        //the next line did not work either, but I though I'd share
        result = item.valid() && result;
    } );
    if (result) {
         $('.first').slideUp();
         $('.second').slideDown();        
    }
}

This is the html form:这是 html 表单:

    <form name="scs_intakebundle_intake" method="post" action="/v2/web/app_dev.php/" novalidate="novalidate">
    <input type="hidden" id="scs_intakebundle_intake__token" name="scs_intakebundle_intake[_token]" value="956WbsNijk_3F_8X_0IGolrcdZaZzar93OwVHAxspyo">

    <div class="first">
        <div class="row">
            <div class="medium-7 columns">
                <input type="text" id="scs_intakebundle_intake_firstName" name="scs_intakebundle_intake[firstName]" required="required" maxlength="255" aria-required="true">
            </div>
        </div>
        <div class="row">
            <div class="medium-7 columns">
                <input type="text" id="scs_intakebundle_intake_lastName" name="scs_intakebundle_intake[lastName]" required="required" maxlength="255" aria-required="true">
            </div>
        </div>
        <div>
            <button type="button" id="scs_intakebundle_intake_next1" name="scs_intakebundle_intake[next1]" style="margin:0;" class="next1 panelButton" onclick="nextPanel();">Find my plan</button>
        </div>
    </div>

    <div class="second">
        <div class="row">
            <div class="medium-7 columns">
                <div id="scs_intakebundle_intake_married">
                    <input type="radio" id="scs_intakebundle_intake_married_0" name="scs_intakebundle_intake[married]" value="0">
                    <label for="scs_intakebundle_intake_married_0">No</label><input type="radio" id="scs_intakebundle_intake_married_1" name="scs_intakebundle_intake[married]" value="1">
                    <label for="scs_intakebundle_intake_married_1">Yes</label></div>
            </div>
        </div>
        <div class="row" style="padding: 1.1rem">
            <button type="button" id="scs_intakebundle_intake_prev1" name="scs_intakebundle_intake[prev1]" style="float:left" class="prev1 panelButton" onclick="previousPanel();">Previous</button>
            <button type="submit" id="scs_intakebundle_intake_submit" name="scs_intakebundle_intake[submit]" class="panelButton">Find my plan</button>
        </div>
    </div>
</form>

I did verify in chrome devtools console, that $('input:visible') does contain the form elements I want to check.我确实在 chrome devtools 控制台中验证过, $('input:visible')确实包含我想要检查的表单元素。 form.validate() does return a $.validator object. form.validate()确实返回一个 $ form.validate()对象。

The validation does work when clicking the final submit button, but not when I try to trigger the validation from the custom function.单击最终提交按钮时验证确实有效,但当我尝试从自定义函数触发验证时无效。

Any ideas?有任何想法吗?

Important : if the form doesn't yet exist in the DOM when you call .validate() then you cannot call .validate() until it does.重要提示:如果当您调用.validate()时该表单尚不存在于 DOM 中,那么您无法调用.validate()直到它存在。 You also cannot call .validate() more than once on the same form.您也不能在同一个表单上多次调用.validate()

Well, I discovered the actual problem and have 2 answers for this post.好吧,我发现了实际问题,并为这篇文章提供了 2 个答案。

  1. I forgot to mention that the div.second was hidden via css.我忘了提到div.second是通过 css 隐藏的。 The anonymous function block tries to find the submit button to add code for the click event.匿名功能块试图找到提交按钮来为点击事件添加代码。 This button is hidden at the time.这个按钮当时是隐藏的。 So, with the function incomplete, validation is not able to run.因此,由于函数不完整,验证无法运行。 When I removed code relevant to the hidden part, all is well.当我删除与隐藏部分相关的代码时,一切都很好。

  2. Being a newbie, I did not realized that I could get access to the validator object by re running the validate code.作为一个新手,我没有意识到我可以通过重新运行验证代码来访问验证器对象。 So adding var validator = $('form").validate(); got me access to the object that was the original question of this post. Even so, it did not work as explained above, regarding the hidden element.因此,添加var validator = $('form").validate();我可以访问作为这篇文章原始问题的对象。即便如此,它也没有像上面解释的那样工作,关于隐藏元素。

Thank you everyone for your assistance.谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM