-
Notifications
You must be signed in to change notification settings - Fork 9
/
VeeValidateAttribute.cs
53 lines (45 loc) · 2.3 KB
/
VeeValidateAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc.DataAnnotations;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.Extensions.Localization;
namespace demo9.Validation
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class VeeValidateAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
=> ValidationResult.Success;
}
public class VeeValidateAttributeAdapter : AttributeAdapterBase<VeeValidateAttribute>
{
public VeeValidateAttributeAdapter(VeeValidateAttribute attribute, IStringLocalizer stringLocalizer) : base(attribute, stringLocalizer) { }
public override void AddValidation(ClientModelValidationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var elementName = context.Attributes["name"];
var attributeValue = $"aspnet:{elementName}";
// Special handling for required rule. Needs to be a part of the base rule definition
// of the v-validate attribute
var isRequired = context.Attributes.ContainsKey("data-val-required");
attributeValue = isRequired ? $"required|{attributeValue}" : attributeValue;
MergeAttribute(context.Attributes, "v-validate", $"'{attributeValue}'");
}
public override string GetErrorMessage(ModelValidationContextBase validationContext)
=> GetErrorMessage(validationContext.ModelMetadata, validationContext.ModelMetadata.GetDisplayName());
}
public class VeeValidateAttributeAdapterProvider : IValidationAttributeAdapterProvider
{
IValidationAttributeAdapterProvider baseProvider = new ValidationAttributeAdapterProvider();
public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
{
if (attribute is VeeValidateAttribute)
return new VeeValidateAttributeAdapter(attribute as VeeValidateAttribute, stringLocalizer);
return baseProvider.GetAttributeAdapter(attribute, stringLocalizer);
}
}
}