Former-commit-id: b3338fd30be0f7934e6df62c9a0b774db2ea1173
Former-commit-id: 0215a318ab681c10f2f7e0f7de03568e25066281
TSXN
wanggang 5 years ago
parent 57aa32f9f9
commit ca07c9ac11

@ -1,10 +1,11 @@
using Jint.Parser.Ast;
using Infrastructure.Resources;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -63,6 +64,8 @@ namespace Infrastructure.Extensions
}
try
{
var factory = controller.HttpContext.RequestServices.GetService<IStringLocalizerFactory>();
var localizer = factory.Create("Resources.Resource", Assembly.GetEntryAssembly().GetName().Name);
dynamic json = new ExpandoObject();
var dictionary = (IDictionary<string, object>)json;
//type
@ -109,13 +112,13 @@ namespace Infrastructure.Extensions
{
json.title = metadata.DisplayName;
}
if (metadata.IsRequired)
{
if (metadata.ModelType != typeof(bool))
{
json.required = true;
}
}
//if (metadata.IsRequired)
//{
// if (metadata.ModelType != typeof(bool))
// {
// json.required = true;
// }
//}
foreach (var attribute in metadata.Attributes.Attributes)
{
if (attribute is DescriptionAttribute descriptionAttribute)
@ -124,7 +127,16 @@ namespace Infrastructure.Extensions
}
else if (attribute is RequiredAttribute requiredAttribute)
{
json.required = requiredAttribute.ErrorMessage;
var localizedString = localizer.GetString(nameof(RequiredAttribute));
var message = requiredAttribute.ErrorMessage;
if (!localizedString.ResourceNotFound)
{
message = string.Format(localizedString.Value, metadata.DisplayName);
}
json.required = new
{
message = localizedString.ResourceNotFound ? requiredAttribute.FormatErrorMessage(metadata.DisplayName) : string.Format(localizedString.Value, metadata.DisplayName)
};
}
else if (attribute is RegularExpressionAttribute regularExpressionAttribute)
{//pattern
@ -212,23 +224,23 @@ namespace Infrastructure.Extensions
}
dynamic properties = new ExpandoObject();
var propertiesDictionary = (IDictionary<string, object>)properties;
var requiredList = new List<string>();
//var requiredList = new List<string>();
if (metadata.IsComplexType && !metadata.IsCollectionType && metadata.Properties.Any())
{
foreach (var item in metadata.Properties)
{
var modelMetadataItem = item as DefaultModelMetadata;
propertiesDictionary[item.PropertyName] = CreateJson(controller, modelMetadataItem);
if (modelMetadataItem.IsRequired)
{
requiredList.Add(item.PropertyName);
}
//if (modelMetadataItem.IsRequired)
//{
// requiredList.Add(item.PropertyName);
//}
}
json.properties = properties;
if (requiredList.Any())
{
json.required = requiredList;
}
// if (requiredList.Any())
// {
// json.required = requiredList;
// }
}
return json;
}

@ -102,9 +102,7 @@ namespace Infrastructure.Web
{
services.AddDbContext<EfDbContext>(options => options.UseNpgsql(_connectionString));
}
//https://github.com/aspnet/Entropy/blob/master/samples/Localization.StarterWeb/Startup.cs
services.AddLocalization(options => options.ResourcesPath = null);
//https://yetawf.com/BlogEntry/Title/AdditionalMetadataAttribute%20Anyone/?BlogEntry=1005
services.AddMvc(o =>
{
o.ModelMetadataDetailsProviders.Add(new AdditionalMetadataProvider());

@ -1,6 +1,8 @@
using Infrastructure.Application;
using Infrastructure.Resources;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Security.AccessControl;
namespace Application.Models
{

@ -117,7 +117,7 @@
}
}
else {
var message = property.required;
var message = property.required.message;
if (!error) {
error = {
key: name,

Loading…
Cancel
Save