如何在自定义验证属性中访问viewmodel的属性值以更改消息? [英] How to access viewmodel's property value in custom validation attribute to alter messages?
问题描述
viewmodel具有许多字符串属性,例如Sample
,如下所示.我的要求是根据我的视图模型中的bool标志显示不同的验证消息.该标志是IsProposer
属性,如下所述:
The viewmodel has many string properties like Sample
as below. My requirement is to show different validation messages depending on a bool flag in my viewmodel. That flag is IsProposer
property as mentioned below:
[SampleAttribute(true, "bla prop", "foo add driver")]
public string Sample { get; set; }
public bool IsProposer { get; set; }
我想创建一个验证属性,以便可以将其放在我的所有字符串属性(必需的验证)上.然后根据该布尔标志的值,我将相应地传递味精.我的自定义验证属性如下:
I thought to create a validation attribute so that I can just place it on all my string properties (required validation). And then depending on the value of that boolean flag, I will pass the msg accordingly. My custom validation attribute is as follows:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
public class SampleAttribute : RequiredAttribute
{
protected string ProposerErrorMessage { get; set; }
protected string AdditionalDriverErrorMessage { get; set; }
protected bool IsProposer { get; set; }
public SampleAttribute(bool isProposer, string propmsg, string adddrivermsg)
{
ProposerErrorMessage = propmsg;
IsProposer = isProposer;
AdditionalDriverErrorMessage = adddrivermsg;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (IsValid(value))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult(IsProposer ? ProposerErrorMessage : AdditionalDriverErrorMessage);
}
}
}
现在的问题是,正如您所看到的,我只是将true作为属性的第一个参数传递.在这里,我需要从viewmodel实例传递Isproposer
属性的值,以便随后可以采取相应的措施.我该如何访问?
Now the issue is, as you can see I am just passing true as first parameter for the attribute. Here, I need to pass the Isproposer
property's value from the viewmodel instance so that I can then act accordingly. How can I access it?
推荐答案
我通过创建这样的属性解决了我的问题:
I solved my problem by creating a attribute like this:
/// <summary>
/// This validation attribute is an extension to RequiredAttribute that can be used to choose either of the two
/// validation messages depending on a property in the context of same model.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
public class RequiredExtensionAttribute : RequiredAttribute
{
private string _errorMessageIfTruthy;
private string _errorMessageIfFalsy;
private string _dependentProperty;
public RequiredExtensionAttribute(string dependentproperty, string errorMessageIfTruthy, string errorMessageIfFalsy)
{
_errorMessageIfTruthy = errorMessageIfTruthy;
_dependentProperty = dependentproperty;
_errorMessageIfFalsy = errorMessageIfFalsy;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var propertyTestedInfo = validationContext.ObjectType.GetProperty(this._dependentProperty);
if (propertyTestedInfo == null)
{
return new ValidationResult(string.Format("unknown property {0}", this._dependentProperty));
}
var propertyTestedValue = propertyTestedInfo.GetValue(validationContext.ObjectInstance, null);
if (IsValid(value))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult((bool)propertyTestedValue ? _errorMessageIfTruthy : _errorMessageIfFalsy);
}
}
}
现在可以在以下模型中使用:
This can now be used in models like:
[RequiredExtensionAttribute("IsProposerViewModel", "Please select your employment status.", "Please select this driver's employment status")]
public string EmploymentStatus { get; set; }
public bool IsProposerViewModel { get; set; }
-其中属性的第一个参数是相关值IsProposerViewModel
.
-where the first parameter for attribute is the IsProposerViewModel
, the dependent value.
这篇关于如何在自定义验证属性中访问viewmodel的属性值以更改消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!