JQuery 验证插件:防止嵌套表单上的验证问题

发布于 2024-09-04 04:57:12 字数 1043 浏览 5 评论 0原文

我有一个使用验证的表单。该表格有一个用于照片上传的部分。最初,此部分包含六个元素,代码如下:

<img class="photo_upload" src="image/app/photo_upload.jpg">

我已将一个函数绑定到 photo_upload 类的单击事件。此函数使用以下代码将图像替换为最小形式: 复制代码

<form onsubmit="startUploadImage();" target="control_target"
enctype="multipart/form-data" method="post" action="index.php">
<input type="hidden" value="add_image" name="service">
<input type="hidden" value="1000000" name="MAX_FILE_SIZE">
<input type="file" size="10" name="photo" id="photo_file_upload"><br>
<button onclick="javascript:cancel_photo_upload();return false;">Cancel</button>
</form>

因此,本质上,单击图像后,我将在原始的经过验证的表单中嵌套一个新表单。现在,当我使用这个新表单并上传图像时,我收到一条错误(重复三次):

validator is undefined
http://host.com/path/index.php
Line 286

这是怎么回事?我的猜测是这个

  1. 提交事件冒泡到外部表单
  2. 当我们对该表单进行验证时,验证被触发,
  3. 验证尝试找到触发它的表单,
  4. 因为我们没有将验证绑定到内部表单,所以它

现在返回“未定义”,我的评估正确吗?不管是与否,我该如何解决这个问题?

I have a form on which I use validation. This form has a section for photo upload. Initially this section contains six elements with the following code:

<img class="photo_upload" src="image/app/photo_upload.jpg">

I have bound a function to the click event for the class of photo_upload. This function replaces the image with a minimal form with this code:
Copy code

<form onsubmit="startUploadImage();" target="control_target"
enctype="multipart/form-data" method="post" action="index.php">
<input type="hidden" value="add_image" name="service">
<input type="hidden" value="1000000" name="MAX_FILE_SIZE">
<input type="file" size="10" name="photo" id="photo_file_upload"><br>
<button onclick="javascript:cancel_photo_upload();return false;">Cancel</button>
</form>

So, essentially, after the image is clicked, I'd have a new form nested in my original, validated form. Now, when I use this new form and upload an image, I receive an error (repeated three times) saying:

validator is undefined
http://host.com/path/index.php
Line 286

What is going on here? My guess is this

  1. Submit event bubbles up to the outer form
  2. As we have validation on that form, validation is triggered,
  3. Validation tries to find the form triggering it,
  4. Since we have not bound validation to the inner form it returns 'undefined'

Now, is my assessment correct? Whether it is or not, how can I solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2024-09-11 04:57:12

HTML 中不允许嵌套表单。您需要使用带有两个提交按钮的单个表单,并设置服务器端表单处理程序,以便它识别哪个按钮是
使用并相应地分支。

Nested forms are not allowed in HTML. You need to use a single form with two submit buttons and to set up the server-side form handler so that it recognizes which button was
used and branches accordingly.

薄暮涼年 2024-09-11 04:57:12

如果您不想更改页面结构,可以通过修补事件处理函数 delegate 来修复此错误。

jquery.validate.js v1.15.0,第 401 - 408 行:

function delegate( event ) {
    var validator = $.data( this.form, "validator" ),
        eventType = "on" + event.type.replace( /^validate/, "" ),
        settings = validator.settings;
    if ( settings[ eventType ] && !$( this ).is( settings.ignore ) ) {
        settings[ eventType ].call( validator, this, event );
    }
}

我的修补版本:

function delegate( event ) {
    if (this.form === event.delegateTarget) {
        var validator = $.data( this.form, "validator" ),
            eventType = "on" + event.type.replace( /^validate/, "" ),
            settings = validator.settings;
        if ( settings[ eventType ] && !$( this ).is( settings.ignore ) ) {
            settings[ eventType ].call( validator, this, event );
        }
    }
}

修补缩小版本(jquery.validate.js 被打包以使用 grunt 和 uglify 进行分发)更困难,但并非不可能。 function delegate(event) 变为 function b(b)

另一种方法是修补绑定delegate时使用的选择器。我认为这是一个更复杂的修复,但浏览器性能会稍微好一些。

If you don't want to alter your page structure, you can fix this error by patching the event handling function delegate.

jquery.validate.js v1.15.0, lines 401 - 408:

function delegate( event ) {
    var validator = $.data( this.form, "validator" ),
        eventType = "on" + event.type.replace( /^validate/, "" ),
        settings = validator.settings;
    if ( settings[ eventType ] && !$( this ).is( settings.ignore ) ) {
        settings[ eventType ].call( validator, this, event );
    }
}

My patched version:

function delegate( event ) {
    if (this.form === event.delegateTarget) {
        var validator = $.data( this.form, "validator" ),
            eventType = "on" + event.type.replace( /^validate/, "" ),
            settings = validator.settings;
        if ( settings[ eventType ] && !$( this ).is( settings.ignore ) ) {
            settings[ eventType ].call( validator, this, event );
        }
    }
}

Patching the minified version (jquery.validate.js is packaged for distribution using grunt with uglify) is harder but not impossible. function delegate(event) becomes function b(b).

An alternative approach would be to patch the selector used when binding delegate. I consider that a more complex fix, but browser performance would be marginally better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文