[英]Custom validation attribute is never validated
I am trying to create a custom validation attribute to only require a field depending on the result of another. 我正在尝试创建一个自定义验证属性,以仅要求一个字段,具体取决于另一个字段的结果。
The problem I am having is that the IsValid block is never called. 我遇到的问题是,从未调用过IsValid块。 The data seems to be getting into the fields and I have been able to check this with a breakpoint. 数据似乎正在进入领域,我已经能够使用断点进行检查。
I tried putting a TryValidateModel(this)
in the OnPostAsync
and this worked through the breakpoint but I could see that another error occurred. 我尝试将TryValidateModel(this)
放在OnPostAsync
并且可以通过断点,但是我可以看到发生了另一个错误。
The requested operation is invalid for DynamicMethod 请求的操作对DynamicMethod无效
Here is my code below. 这是下面的代码。 Any help would be appreciated. 任何帮助,将不胜感激。
public class PageOneModel : PageModel
{
[BindProperty]
public bool CompanyHouseToggle { get; set; }
[BindProperty]
[StringLength(60, MinimumLength = 3)]
[RequiredIf("CompanyHouseToggle", desiredvalue: "true")]
public string CompanyNumber { get; set; }
[BindProperty]
[StringLength(60, MinimumLength = 3)]
public string OrganisationName { get; set; }
[BindProperty]
[RegularExpression(pattern: "(GB)?([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3})", ErrorMessage = "This VAT number is not recognised")]
public string VatNumber { get; set; }
public void OnGet()
{
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
RedirectToPage("2");
}
}
public class RequiredIfAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
{
private String PropertyName { get; set; }
private Object DesiredValue { get; set; }
public RequiredIfAttribute(String propertyName, Object desiredvalue)
{
this.PropertyName = propertyName;
this.DesiredValue = desiredvalue;
}
protected override ValidationResult IsValid(object value, ValidationContext context)
{
var property = context.ObjectType.GetProperty(PropertyName);
if (property == null)
throw new ArgumentException("Property with this name not found");
// Just for testing purposes.
return new ValidationResult(ErrorMessage);
}
}
I suggest you to inherit from the ReuiredAttribute. 我建议您从ReuiredAttribute继承。 It fully works for me. 它完全适合我。
public class RequiredUnlessDeletingAttribute : RequiredAttribute
{
string DeletingProperty;
/// <summary>
/// Check if the object is going to be deleted skip the validation.
/// </summary>
/// <param name="deletingProperty">The boolean property`s name which shows the object will be deleted.</param>
public RequiredUnlessDeletingAttribute(string deletingProperty = "MustBeDeleted") =>
DeletingProperty = deletingProperty;
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var property = validationContext.ObjectType.GetProperty(deletingProperty);
if ((bool)property.GetValue(validationContext.ObjectInstance))
return ValidationResult.Success;
return base.IsValid(value, validationContext);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.