JQuery 验证自定义方法在 jquery.load() 之后不起作用
JQuery Validate custom method not working after jquery.load()
我尝试获取 JQueryValidate 自定义验证属性的尝试在使用 jquery.load 时似乎不起作用。这是我的代码:
CreateViewModel.cs
public class CreateViewModel
{
[DisplayName("Artwork")]
public int ArtworkId { get; set; }
[AtLeastOne("selectiongroup")]
public bool IsPromo { get; set; }
[AtLeastOne("selectiongroup")]
public bool IsUpc { get; set; }
[AtLeastOne("selectiongroup")]
public bool IsCoupon { get; set; }
public SelectList ArtworkSelectList { get; set; }
}
AtLeastOneAttribute.cs
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class AtLeastOneAttribute : ValidationAttribute, IClientValidatable
{
public string GroupName { get; private set; }
public AtLeastOneAttribute(string groupName)
{
GroupName = groupName;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage = "At least one of Coupon or Upc or Promo is required.",
ValidationType = "atleastone",
ValidationParameters =
{
new KeyValuePair<string, object>("groupname", GroupName),
}
};
}
}
Page.cshtml
<form method="POST" id="signCreationForm">
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
@Html.LabelFor(model => model.ArtworkId)
@Html.DropDownListFor(model => model.ArtworkId, Model.ArtworkSelectList, "Select an Artwork")
</div>
</div>
<div class="row">
<div class="col-lg-6"></div>
<div>@Html.ValidationMessageFor(model => model.ArtworkId)</div>
</div>
<div class="row">
<div class="col-lg-10">
<label>Associated With:</label>
</div>
</div>
<div class="row">
<div class="col-lg-4">
@Html.LabelFor(model => model.IsUpc, "UPC")
@Html.CheckBoxFor(model => model.IsUpc)
@Html.ValidationMessageFor(model => model.IsUpc)
</div>
<div class="col-lg-4">
@Html.LabelFor(model => model.IsCoupon, "Coupon")
@Html.CheckBoxFor(model => model.IsCoupon)
@Html.ValidationMessageFor(model => model.IsCoupon)
</div>
<div class="col-lg-4">
@Html.LabelFor(model => model.IsPromo, "Promotion")
@Html.CheckBoxFor(model => model.IsPromo)
@Html.ValidationMessageFor(model => model.IsPromo)
</div>
</div>
</div>
</form>
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$.validator.addMethod("atleastone", function (value, element, params) {
return $("[data-val-atleastone-groupname="+params+"]:checked").length > 0;
});
$.validator.unobtrusive.adapters.add("atleastone", ["groupname"], function (options) {
options.rules["atleastone"] = options.params.groupname;
options.messages["atleastone"] = options.message;
});
</script>
正在调用Page.cshtml(javascript 正在调用它)
$("#create-ad[data-link]").on("click", function() {
var link = $(this).data("link");
$createDialog.load(link, function() {
$createDialog.dialog("open");
$.validator.unobtrusive.parse($("#signCreationForm"));
});
});
现在,此代码在单独的项目中运行。在这个项目中,部分视图是通过 jquery.load() 事件加载的,由于某种原因,这导致自定义方法永远不会执行。我知道这一点,因为警报通常会针对每个用 atleastone 属性装饰的属性触发一次。
使用带有自定义 jqueryvalidate 验证的 jquery 加载时,在 Page.cshtml 上有什么特别需要做的吗?
请将您的自定义方法放入 jquery 文档准备就绪;;;;;;
对于 I.E.
<script type="text/javascript">
$(document).ready(function(){
$.validator.addMethod("atleastone", function (value, element, params) {
alert("halp");
return $(".grouped:checked").length > 0;
});
$.validator.unobtrusive.adapters.add("atleastone", ["groupname"], function (options) {
alert("halp");
options.rules["atleastone"] = "#" + options.params.groupname;
options.messages["atleastone"] = options.message;
});
});
</script>
此场景答案的建议副本 得到了问题的前半部分,即添加 $.validator.unobtrusive.adapters.add 无法正常工作。然而,在那之后的其余代码也不是 运行。
@Bhavin Solanki 关于在 调用页面 上包含验证文件的评论修复了另一半,即提交代码不是 运行。
我尝试获取 JQueryValidate 自定义验证属性的尝试在使用 jquery.load 时似乎不起作用。这是我的代码:
CreateViewModel.cs
public class CreateViewModel
{
[DisplayName("Artwork")]
public int ArtworkId { get; set; }
[AtLeastOne("selectiongroup")]
public bool IsPromo { get; set; }
[AtLeastOne("selectiongroup")]
public bool IsUpc { get; set; }
[AtLeastOne("selectiongroup")]
public bool IsCoupon { get; set; }
public SelectList ArtworkSelectList { get; set; }
}
AtLeastOneAttribute.cs
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class AtLeastOneAttribute : ValidationAttribute, IClientValidatable
{
public string GroupName { get; private set; }
public AtLeastOneAttribute(string groupName)
{
GroupName = groupName;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage = "At least one of Coupon or Upc or Promo is required.",
ValidationType = "atleastone",
ValidationParameters =
{
new KeyValuePair<string, object>("groupname", GroupName),
}
};
}
}
Page.cshtml
<form method="POST" id="signCreationForm">
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
@Html.LabelFor(model => model.ArtworkId)
@Html.DropDownListFor(model => model.ArtworkId, Model.ArtworkSelectList, "Select an Artwork")
</div>
</div>
<div class="row">
<div class="col-lg-6"></div>
<div>@Html.ValidationMessageFor(model => model.ArtworkId)</div>
</div>
<div class="row">
<div class="col-lg-10">
<label>Associated With:</label>
</div>
</div>
<div class="row">
<div class="col-lg-4">
@Html.LabelFor(model => model.IsUpc, "UPC")
@Html.CheckBoxFor(model => model.IsUpc)
@Html.ValidationMessageFor(model => model.IsUpc)
</div>
<div class="col-lg-4">
@Html.LabelFor(model => model.IsCoupon, "Coupon")
@Html.CheckBoxFor(model => model.IsCoupon)
@Html.ValidationMessageFor(model => model.IsCoupon)
</div>
<div class="col-lg-4">
@Html.LabelFor(model => model.IsPromo, "Promotion")
@Html.CheckBoxFor(model => model.IsPromo)
@Html.ValidationMessageFor(model => model.IsPromo)
</div>
</div>
</div>
</form>
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$.validator.addMethod("atleastone", function (value, element, params) {
return $("[data-val-atleastone-groupname="+params+"]:checked").length > 0;
});
$.validator.unobtrusive.adapters.add("atleastone", ["groupname"], function (options) {
options.rules["atleastone"] = options.params.groupname;
options.messages["atleastone"] = options.message;
});
</script>
正在调用Page.cshtml(javascript 正在调用它)
$("#create-ad[data-link]").on("click", function() {
var link = $(this).data("link");
$createDialog.load(link, function() {
$createDialog.dialog("open");
$.validator.unobtrusive.parse($("#signCreationForm"));
});
});
现在,此代码在单独的项目中运行。在这个项目中,部分视图是通过 jquery.load() 事件加载的,由于某种原因,这导致自定义方法永远不会执行。我知道这一点,因为警报通常会针对每个用 atleastone 属性装饰的属性触发一次。
使用带有自定义 jqueryvalidate 验证的 jquery 加载时,在 Page.cshtml 上有什么特别需要做的吗?
请将您的自定义方法放入 jquery 文档准备就绪;;;;;;
对于 I.E.
<script type="text/javascript">
$(document).ready(function(){
$.validator.addMethod("atleastone", function (value, element, params) {
alert("halp");
return $(".grouped:checked").length > 0;
});
$.validator.unobtrusive.adapters.add("atleastone", ["groupname"], function (options) {
alert("halp");
options.rules["atleastone"] = "#" + options.params.groupname;
options.messages["atleastone"] = options.message;
});
});
</script>
此场景答案的建议副本 得到了问题的前半部分,即添加 $.validator.unobtrusive.adapters.add 无法正常工作。然而,在那之后的其余代码也不是 运行。
@Bhavin Solanki 关于在 调用页面 上包含验证文件的评论修复了另一半,即提交代码不是 运行。