使用 Entity Framework 在 MVC 中进行跨字段验证
Cross Field Validation in MVC with Entity Framework
我正在尝试在 MVC 5 中完成一些跨域验证。具体来说,如果选中一个复选框,则需要一个自由格式响应区域,这通常是可选的。服务器端似乎工作正常,但我无法让我的客户端验证工作。
我的 RequiredIfAttribute Class 和 Validator
从其他解决方案开始工作
public class RequiredIfAttribute : ValidationAttribute{
public string DependentUpon { get; set; }
public string CallerValue { get; set; }
public RequiredIfAttribute(string dependentUpon)
{
this.DependentUpon = dependentUpon;
//this.Value = null;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
object instance = validationContext.ObjectInstance;
Type type = instance.GetType();
var property = type.GetProperty(DependentUpon);
var targetValue = property.GetValue(instance);
if (value == null)
CallerValue = "";
else
CallerValue = value.ToString();
if(Convert.ToBoolean(targetValue) == true && String.IsNullOrWhiteSpace(CallerValue))
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
return ValidationResult.Success;
}}
public class RequiredIfValidator : DataAnnotationsModelValidator<RequiredIfAttribute>{
public RequiredIfValidator(ModelMetadata metadata, ControllerContext context, RequiredIfAttribute attribute)
: base(metadata, context, attribute)
{ }
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
var rule = new ModelClientValidationRule
{
ValidationType = "requiredifvalidation",
ErrorMessage = Attribute.FormatErrorMessage(Metadata.PropertyName),
};
rule.ValidationParameters.Add("dependentupon", Attribute.DependentUpon);
return new[] { rule };
}}
我有我的 Global.asax 注册
DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(RequiredIfAttribute), typeof(RequiredIfValidator));
标记到我的元数据
[StringLength(1000)]
[DataType(DataType.MultilineText)]
[Display(Name = "Investigator Notes")]
[RequiredIf("NotToBeProcessed")]
public string InvestigatorNotes { get; set; }
和一些来自旧 post 的 JS 尝试将验证传播到客户端
jQuery.validator.addMethod("requiredifvalidation", function (value, element, params) {
if (this.optional(element)) {
return true;
}
var dependentUponControl = $("#" + params.dependentUpon);
if (dependentUponControl == null) {
return false;
}
var dependentUponValue = dependentUponControl[0].value;
return dependentUponValue == value;});
function testConditionEqual(element, params) {
/* Find control for other property */
var dependentUponControl = $("#" + params.dependentUpon);
if (dependentUponControl == null) {
return false;
}
var dependentUponValue;
if (dependentUponControl[0].type == "checkbox") {
dependentUponValue = (dependentUponControl[0].checked) ? "True" : "False";
} else {
dependentUponValue = dependentUponControl[0].value;
}
return dependentUponValue == params.comparand;}
服务器端验证适用并且不会通过在填写表单并重新加载页面时保存的模型,但我的表单页面从不更新显示我的验证
@Html.ValidationMessageFor(model => model.InvestigatorNotes, "", new { @class = "text-danger" })
面积
我之前使用了 here 中的 RequiredIf 属性。效果很好。它有服务器端和客户端代码,用法和你的完全一样:
public class Category
{
[RequiredIf("IsActive", true)]
public string Code { get; set; }
public bool IsActive { get; set; }
}
我正在尝试在 MVC 5 中完成一些跨域验证。具体来说,如果选中一个复选框,则需要一个自由格式响应区域,这通常是可选的。服务器端似乎工作正常,但我无法让我的客户端验证工作。
我的 RequiredIfAttribute Class 和 Validator
从其他解决方案开始工作public class RequiredIfAttribute : ValidationAttribute{
public string DependentUpon { get; set; }
public string CallerValue { get; set; }
public RequiredIfAttribute(string dependentUpon)
{
this.DependentUpon = dependentUpon;
//this.Value = null;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
object instance = validationContext.ObjectInstance;
Type type = instance.GetType();
var property = type.GetProperty(DependentUpon);
var targetValue = property.GetValue(instance);
if (value == null)
CallerValue = "";
else
CallerValue = value.ToString();
if(Convert.ToBoolean(targetValue) == true && String.IsNullOrWhiteSpace(CallerValue))
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
return ValidationResult.Success;
}}
public class RequiredIfValidator : DataAnnotationsModelValidator<RequiredIfAttribute>{
public RequiredIfValidator(ModelMetadata metadata, ControllerContext context, RequiredIfAttribute attribute)
: base(metadata, context, attribute)
{ }
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
var rule = new ModelClientValidationRule
{
ValidationType = "requiredifvalidation",
ErrorMessage = Attribute.FormatErrorMessage(Metadata.PropertyName),
};
rule.ValidationParameters.Add("dependentupon", Attribute.DependentUpon);
return new[] { rule };
}}
我有我的 Global.asax 注册
DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(RequiredIfAttribute), typeof(RequiredIfValidator));
标记到我的元数据
[StringLength(1000)]
[DataType(DataType.MultilineText)]
[Display(Name = "Investigator Notes")]
[RequiredIf("NotToBeProcessed")]
public string InvestigatorNotes { get; set; }
和一些来自旧 post 的 JS 尝试将验证传播到客户端
jQuery.validator.addMethod("requiredifvalidation", function (value, element, params) {
if (this.optional(element)) {
return true;
}
var dependentUponControl = $("#" + params.dependentUpon);
if (dependentUponControl == null) {
return false;
}
var dependentUponValue = dependentUponControl[0].value;
return dependentUponValue == value;});
function testConditionEqual(element, params) {
/* Find control for other property */
var dependentUponControl = $("#" + params.dependentUpon);
if (dependentUponControl == null) {
return false;
}
var dependentUponValue;
if (dependentUponControl[0].type == "checkbox") {
dependentUponValue = (dependentUponControl[0].checked) ? "True" : "False";
} else {
dependentUponValue = dependentUponControl[0].value;
}
return dependentUponValue == params.comparand;}
服务器端验证适用并且不会通过在填写表单并重新加载页面时保存的模型,但我的表单页面从不更新显示我的验证
@Html.ValidationMessageFor(model => model.InvestigatorNotes, "", new { @class = "text-danger" })
面积
我之前使用了 here 中的 RequiredIf 属性。效果很好。它有服务器端和客户端代码,用法和你的完全一样:
public class Category
{
[RequiredIf("IsActive", true)]
public string Code { get; set; }
public bool IsActive { get; set; }
}