本文整理汇总了C#中System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationAttribute.GetValidationResult方法的具体用法?C# ValidationAttribute.GetValidationResult怎么用?C# ValidationAttribute.GetValidationResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ComponentModel.DataAnnotations.ValidationAttribute
的用法示例。
在下文中一共展示了ValidationAttribute.GetValidationResult方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
public IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor, NancyContext context)
{
var validationContext = new ValidationContext(instance, null, null)
{
MemberName = ((MatchAttribute)attribute).SourceProperty
};
var result = attribute.GetValidationResult(instance, validationContext);
if(result != null)
{
yield return new ModelValidationError(result.MemberNames, attribute.ErrorMessage);
}
yield break;
}
示例2: Validate
/// <summary>
/// Validates the given instance.
/// </summary>
/// <param name="instance">The instance that should be validated.</param>
/// <param name="attribute">The <see cref="ValidationAttribute"/> that should be handled.</param>
/// <param name="descriptor">A <see cref="PropertyDescriptor"/> instance for the property that is being validated.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns>
public virtual IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor)
{
var context =
new ValidationContext(instance, null, null)
{
MemberName = descriptor == null ? null : descriptor.Name
};
if(descriptor != null)
{
instance = descriptor.GetValue(instance);
}
var result =
attribute.GetValidationResult(instance, context);
if (result != null)
{
yield return new ModelValidationError(result.MemberNames, string.Join(" ", result.MemberNames.Select(attribute.FormatErrorMessage)));
}
}
示例3: validate
private bool validate(ValidationAttribute v, object value)
{
return v.GetValidationResult(value, new ValidationContext(ValidationContext, null, null)) == ValidationResult.Success;
}
示例4: TryValidate
/// <summary>
/// Tests whether a value is valid against a single <see cref="ValidationAttribute"/> using the <see cref="ValidationContext"/>.
/// </summary>
/// <param name="value">The value to be tested for validity.</param>
/// <param name="validationContext">Describes the property member to validate.</param>
/// <param name="attribute">The validation attribute to test.</param>
/// <param name="validationError">The validation error that occurs during validation. Will be <c>null</c> when the return value is <c>true</c>.</param>
/// <returns><c>true</c> if the value is valid.</returns>
/// <exception cref="ArgumentNullException">When <paramref name="validationContext"/> is null.</exception>
private static bool TryValidate(object value, ValidationContext validationContext, ValidationAttribute attribute, out ValidationError validationError)
{
if (validationContext == null) {
throw new ArgumentNullException("validationContext");
}
ValidationResult validationResult = attribute.GetValidationResult(value, validationContext);
if (validationResult != ValidationResult.Success) {
validationError = new ValidationError(attribute, value, validationResult);
return false;
}
validationError = null;
return true;
}
示例5: Validate
/// <summary>
/// Validates the given instance.
/// </summary>
/// <param name="instance">The instance that should be validated.</param>
/// <param name="attribute">The <see cref="ValidationAttribute"/> that should be handled.</param>
/// <param name="descriptor">A <see cref="PropertyDescriptor"/> instance for the property that is being validated.</param>
/// <param name="context">The <see cref="NancyContext"/> of the current request.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns>
public virtual IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor, NancyContext context)
{
var validationContext =
new ValidationContext(instance, null, null)
{
MemberName = descriptor == null ? null : descriptor.Name
};
// When running on Mono the Display attribute is not auto populated so for now we do it ourselves
if (IsRunningOnMono)
{
var displayName = this.GetDisplayNameForMember(instance, validationContext.MemberName);
if (!string.IsNullOrEmpty(displayName))
{
validationContext.DisplayName = displayName;
}
}
if (descriptor != null)
{
// Display(Name) will auto populate the context, while DisplayName() needs to be manually set
if (validationContext.MemberName == validationContext.DisplayName && !string.IsNullOrEmpty(descriptor.DisplayName))
{
validationContext.DisplayName = descriptor.DisplayName;
}
instance = descriptor.GetValue(instance);
}
var result =
attribute.GetValidationResult(instance, validationContext);
if (result != null)
{
yield return this.GetValidationError(result, validationContext, attribute);
}
}
示例6: GetFinalMemberName
private static string GetFinalMemberName(ValidationAttribute attrib, object contextModel, object mamberValue, string givenMemberName, string givenDisplayName)
{
return attrib.GetValidationResult(mamberValue, new ValidationContext(contextModel)
{
MemberName = givenMemberName,
DisplayName = givenDisplayName
})?.MemberNames.Single();
}