-
Notifications
You must be signed in to change notification settings - Fork 1
/
TransValidationAttribute.cs
47 lines (39 loc) · 1.54 KB
/
TransValidationAttribute.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
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
namespace DotStd
{
/// <summary>
/// Try to translate the validation error messages based on some unknown context.
/// e.g. make them appropriate to the HttpContext that is current for the thread.
/// </summary>
public static class TransValidationAttribute
{
public static Func<ITranslatorProvider1?>? _GetTranslator; // My application should provide this. singleton.
public static ITranslatorProvider1? GetTranslatorProvider()
{
// Get/Make the ITranslatorProvider for the ASP HttpContext that is appropriate for my thread/session.
if (_GetTranslator == null)
return null;
return _GetTranslator();
}
}
public class TransRequiredAttribute : RequiredAttribute
{
// use [TransRequired] not [Required]
public override string FormatErrorMessage(string name)
{
// override ValidationAttribute
ITranslatorProvider1? trans = TransValidationAttribute.GetTranslatorProvider();
if (trans != null)
{
Task<string> task = trans.TranslateAsync(base.ErrorMessageString ?? "The {0} field is required.");
task.Wait(); // Wait for async.
string errorMsg = task.Result;
return string.Format(errorMsg, name);
}
return base.FormatErrorMessage(name);
}
}
// TODO OTHER VALIDATION ATTRIBUTES ??
}