Unobtrusive验证为有效手动显示错误

Unobtrusive验证为有效手动显示错误

本文介绍了jQuery Unobtrusive验证为有效手动显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此处此处中的示例将链接手动添加了错误到输入中这里,但是当我在添加错误变成真的吗?

I have added errors manually to my input using the link in here Here and the example in Here but when I try $("#myshowErrors").valid() after I added the errors it becomes true?

这是我的代码

var validator = $( "#myshowErrors" ).validate();
validator.showErrors({
  "firstname": "I know that your firstname is Pete, Pete!"
});

我无法使客户端验证失败.如何使form.valid()返回false?

I am not able to make the client validation fail. How can I make form.valid() to return false?

我不想手动制作form.valid()=false;,我希望仅通过设置错误来解决.

I don't want to make form.valid()=false; manually I want that to be taken care of by just setting the errors.

推荐答案

由于您的问题中缺少代码,目前尚不清楚,但是我认为您可能会误解了在何处附加 .valid()方法.

It's entirely unclear by the lack of code in your question, but I think you might misunderstand where to attach the .valid() method.

此方法仅附加到表示单个 <form>元素的选择器...

This method only gets attached to a selector representing a single <form> element...

$('#myform').valid();  // triggers validation on the entire form

或者指向表示单个表单输入元素的任何选择器...

Or to any selector that represents a single form input element...

$('#myform input[name="myinput"]').valid();  // triggers validation on one input element

调用.valid()时,将在所选元素上触发验证,并显示错误消息,并且该方法将返回truefalse.

When .valid() is called, validation is triggered on the selected element, error message(s) is/are displayed, and the method will return true or false.

在不使用jQuery .each()的情况下,不能将.valid()方法附加到表示多个元素或元素集合的选择器中.

The .valid() method can never be attached to a selector that represents more than one element, or a collection of elements without also using a jQuery .each().

showErrors仅用于显示现有错误.您不能通过调用showErrors使字段无效".仅根据该字段中包含的内容,这些字段是有效的还是无效的.

showErrors is just for showing existing errors. You cannot "invalidate" a field by calling showErrors. Fields are either valid or invalid based solely on what is contained within that field.

您已使用非干扰性验证标记了该问题.由于jQuery验证是通过Unobtrusive Validation插件自动处理的,因此您不允许调用自己的.validate()方法实例.这是因为Unobtrusive插件会自动构造并调用.validate().一旦被调用,插件将被初始化,无法再次调用,并且所有后续调用都将被忽略.

You've tagged the question with Unobtrusive Validation. Since jQuery Validation is handled automatically via the Unobtrusive Validation plugin, you are not allowed to call your own instance of the .validate() method. That's because the Unobtrusive plugin automatically constructs and calls .validate(). Once it's called, the plugin is initialized, it cannot be called again, and all subsequent calls are always ignored.

这篇关于jQuery Unobtrusive验证为有效手动显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:09