using System; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Reflection; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Options; namespace gehGassi.Web.Helper { /// /// Prüft ob ein Feld angegeben werden muss, wenn es sich um die Übersetzung der Standard-Sprache ist. /// [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class RequiredIfDefaultLanguage : ValidationAttribute, IClientModelValidator { public string OtherProperty { get; } public RequiredIfDefaultLanguage(string otherProperty) { OtherProperty = otherProperty; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var localizationOptions = (IOptions)validationContext.GetService(typeof(IOptions)); var defaultLanguage = localizationOptions.Value.DefaultCulture; PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherProperty); if (otherPropertyInfo == null) { return new ValidationResult($"Unknown property {OtherProperty}"); } var otherLanguage = string.Empty; object otherValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null); if (otherValue != null) { otherLanguage = otherValue.ToString(); } if(otherLanguage == defaultLanguage && value == null) return new ValidationResult(string.Format(ErrorMessage, validationContext.DisplayName)); var stringValue = ""; if(value != null) stringValue = value.ToString(); if (otherLanguage == defaultLanguage && string.IsNullOrWhiteSpace(stringValue)) return new ValidationResult(string.Format(ErrorMessage, validationContext.DisplayName)); return ValidationResult.Success; } public void AddValidation(ClientModelValidationContext context) { var localizerFactory = (IStringLocalizerFactory)context.ActionContext.HttpContext.RequestServices.GetService(typeof(IStringLocalizerFactory)); var localizationOptions = (IOptions)context.ActionContext.HttpContext.RequestServices.GetService(typeof(IOptions)); var defaultLanguage = localizationOptions.Value.DefaultCulture; var localizer = localizerFactory.Create("Annotations", "Local"); var viewContext = context.ActionContext as ViewContext; var otherPropertyFullName = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(OtherProperty); var otherPropertyFullId = otherPropertyFullName.Replace(".", "_"); var prefix = ""; if (context.ModelMetadata.ContainerType != null && context.ModelMetadata.ContainerMetadata != null) { foreach (var propertyInfo in context.ModelMetadata.ContainerMetadata.GetType().GetProperties()) { if (propertyInfo.PropertyType == context.ModelMetadata.ContainerType) { prefix = propertyInfo.Name; break; } } } if (!context.Attributes.ContainsKey("data-val")) context.Attributes.Add("data-val", "true"); if (!context.Attributes.ContainsKey("data-val-requiredif-defaultlangauge")) context.Attributes.Add("data-val-requiredif-defaultlangauge", string.Format((IFormatProvider)CultureInfo.CurrentCulture, localizer["Err_Required"], context.ModelMetadata.GetDisplayName())); if (!string.IsNullOrWhiteSpace(prefix)) { if (!context.Attributes.ContainsKey("data-val-requiredif-defaultlangauge-other")) context.Attributes.Add("data-val-requiredif-defaultlangauge-other", prefix + "." + otherPropertyFullName); // data-val-requiredif-other } else { if (!context.Attributes.ContainsKey("data-val-requiredif-defaultlangauge-other")) context.Attributes.Add("data-val-requiredif-defaultlangauge-other", otherPropertyFullName); // data-val-requiredif-other } if (!context.Attributes.ContainsKey("data-val-requiredif-defaultlangauge-otherval")) context.Attributes.Add("data-val-requiredif-defaultlangauge-otherval", defaultLanguage); // data-val-requiredif-other } } }