简体   繁体   English

在外部提交按钮中使用“formnovalidate”进行 jQuery 验证<form>

[英]jQuery Validation with "formnovalidate" in submit button outside the <form>

I have an edit form using the jQuery Validation plugin.我有一个使用 jQuery Validation 插件的编辑表单。 It's part of an ASP.NET Core project, also using ASP.NET's Unobtrusive Validation plugin.它是 ASP.NET Core 项目的一部分,也使用 ASP.NET 的 Unobtrusive Validation 插件。 The form has two submit buttons, posting to different handlers server-side:该表单有两个提交按钮,发布到服务器端的不同处理程序:

  • Submit changes提交更改
  • Delete entry删除条目

"Submit changes" is working fine, however I'd like the "Delete" button to skip any client-side validation. “提交更改”工作正常,但我希望“删除”按钮跳过任何客户端验证。 Currently it won't post the form if any required fields are missing (or any other validation condition doesn't pass).目前,如果缺少任何必填字段(或任何其他验证条件未通过),它将不会发布表单。

I've tried the HTML5 formnovalidate attribute on the "Delete" button without success.我在“删除”按钮上尝试了 HTML5 formnovalidate属性,但没有成功。 Is there an equivalent feature in the jQuery Validation plugin? jQuery Validation 插件中是否有等效功能? If not, how would you bypass validation only for a specific submit button?如果不是,您将如何绕过仅针对特定提交按钮的验证?

EDIT:编辑:

The "Delete" button is actually outside the <form> tag, but referencing the form by ID through the form attribute: “删除”按钮实际上在<form>标记之外,但通过form属性通过 ID 引用表单:

<form id="my-form">
  <!-- form fields and submit button here -->
</form>
<button type="submit" form="my-form" noformvalidate asp-page-handler="Delete">
  Delete entry
</button>

I've found that when moving the "Delete" button inside the <form> , the noformvalidate attribute works as expected.我发现在<form>中移动“删除”按钮时, noformvalidate属性按预期工作。 I would really like to keep this button outside the <form> tag (due to the page's layout), though I might be able to work around it if there's no other way.我真的很想将此按钮保留在<form>标记之外(由于页面的布局),但如果没有其他方法,我可能可以解决它。

Any ideas on how to make it skip validation while placed outside the form?关于如何使它在放置在表单之外时跳过验证的任何想法?

The problem you have seems to be summarized in this issue :您遇到的问题似乎已在此问题中进行了总结:

Typical save vs submit button where save does not validate and submit does.典型的保存与提交按钮,其中保存不验证而提交。 Save button is declared with the formnovalidate attrribute.保存按钮使用 formnovalidate 属性声明。 Only thing is that these buttons are outside of the form itself.唯一的问题是这些按钮在表单本身之外。

See, the plugin expects the submit buttons to be inside your form.看,插件希望提交按钮在您的表单中。 It actually still handles both 'preventing' flags - cancel class and formnovalidate attribute - within click handler propagated from buttons to the top of the form ( source ):它实际上仍然处理“预防”标志 - cancel类和formnovalidate属性 - 在单击处理程序中从按钮传播到表单顶部( source ):

// "this" is jQuery-wrapped HTMLFormElement with validator attaching
this.on( "click.validate", ":submit", function( event ) {
  // Track the used submit button to properly handle scripted
  // submits later.
  validator.submitButton = event.currentTarget;

  // Allow suppressing validation by adding a cancel class to the submit button
  if ( $( this ).hasClass( "cancel" ) ) {
    validator.cancelSubmit = true;
  }

  // Allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
  if ( $( this ).attr( "formnovalidate" ) !== undefined ) {
    validator.cancelSubmit = true;
  }
} );

... which clearly doesn't work if buttons are outside of form DOM hierarchy, like in your case. ...如果按钮在表单DOM层次结构之外,这显然不起作用,就像你的情况一样。 Only submit.validate handler is fired, but it expects to check validator.cancelSubmit flag (and set it to false if it's truthy).只有submit.validate处理程序被触发,但它希望检查validator.cancelSubmit标志(如果它是真实的,则将其设置为 false)。

One idea that comes to mind is to place your own click handler on Delete button that will override that flag.想到的一个想法是将您自己的单击处理程序放在将覆盖该标志的Delete按钮上。 Validator instance is accessible through form $.data, as usually with jQuery plugins:验证器实例可以通过表单 $.data 访问,就像通常使用 jQuery 插件一样:

const validator = $.data(form, 'validator');

Perhaps you are looking for something like this:也许您正在寻找这样的东西:

var $frm = $('#my-form')
    , frm = $frm[0]
    , $btnsubmit = $('button[type="submit"]');
frm.addEventListener('submit', (evt) => {
    var skipValidation = evt.submitter.hasAttribute('formnovalidate')
        , validator = $frm.data('validator');
    skipValidation && validator !== undefined
        ? (validator.cancelSubmit = true)
        : ($frm.valid() && $btnsubmit.prop('disabled', true));
})

here I'm using a mix of vanilla JS and JQuery because not all submit-event properties are available in JQuery and is not so recommended to access JQuery data using vanilla JS.在这里,我混合使用vanilla JS 和 JQuery,因为并非所有提交事件属性都在 JQuery 中可用,因此不建议使用vanilla JS 访问 JQuery data In this case, there is no matter where are declared your submit-buttons but... you should be sure to execute the above script before $.validator.unobtrusive.parse(document) that mean before validator.unobtrusive become obtrusive .在这种情况下,无论在哪里声明您的提交按钮,但是......您应该确保在$.validator.unobtrusive.parse(document)之前执行上述脚本,这意味着在validator.unobtrusive变得引人注目之前。

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

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