diff --git a/src/Altinn.App.Core/Features/Validation/ValidationAppSI.cs b/src/Altinn.App.Core/Features/Validation/ValidationAppSI.cs index 6b470d90d..aa5c78e6d 100644 --- a/src/Altinn.App.Core/Features/Validation/ValidationAppSI.cs +++ b/src/Altinn.App.Core/Features/Validation/ValidationAppSI.cs @@ -238,24 +238,30 @@ public async Task> ValidateDataElement(Instance instance, messages.AddRange(layoutErrors); } - // run Standard mvc validation using the System.ComponentModel.DataAnnotations - ModelStateDictionary validationResults = new ModelStateDictionary(); + // Run Standard mvc validation using the System.ComponentModel.DataAnnotations + ModelStateDictionary dataModelValidationResults = new ModelStateDictionary(); var actionContext = new ActionContext( _httpContextAccessor.HttpContext, new Microsoft.AspNetCore.Routing.RouteData(), new ActionDescriptor(), - validationResults); + dataModelValidationResults); ValidationStateDictionary validationState = new ValidationStateDictionary(); _objectModelValidator.Validate(actionContext, validationState, null, data); + if (!dataModelValidationResults.IsValid) + { + messages.AddRange(MapModelStateToIssueList(actionContext.ModelState, ValidationIssueSources.ModelState, instance, dataElement.Id, data.GetType())); + } + // Call custom validation from the IInstanceValidator - await _instanceValidator.ValidateData(data, validationResults); + ModelStateDictionary customValidationResults = new ModelStateDictionary(); + await _instanceValidator.ValidateData(data, customValidationResults); - // Add the validation messages from System.ComponentModel.DataAnnotations and IInstanceValidator to the return list - if (!validationResults.IsValid) + if (!customValidationResults.IsValid) { - messages.AddRange(MapModelStateToIssueList(actionContext.ModelState, instance, dataElement.Id, data.GetType())); + messages.AddRange(MapModelStateToIssueList(customValidationResults, ValidationIssueSources.Custom, instance, dataElement.Id, data.GetType())); } + } return messages; @@ -263,6 +269,7 @@ public async Task> ValidateDataElement(Instance instance, private List MapModelStateToIssueList( ModelStateDictionary modelState, + string source, Instance instance, string dataElementId, Type modelType) @@ -282,7 +289,7 @@ private List MapModelStateToIssueList( { InstanceId = instance.Id, DataElementId = dataElementId, - Source = ValidationIssueSources.ModelState, + Source = source, Code = severityAndMessage.Message, Field = ModelKeyToField(modelKey, modelType)!, Severity = severityAndMessage.Severity, diff --git a/src/Altinn.App.Core/Models/Validation/ValidationIssueSource.cs b/src/Altinn.App.Core/Models/Validation/ValidationIssueSource.cs index 00d493a11..532119033 100644 --- a/src/Altinn.App.Core/Models/Validation/ValidationIssueSource.cs +++ b/src/Altinn.App.Core/Models/Validation/ValidationIssueSource.cs @@ -20,5 +20,10 @@ public static class ValidationIssueSources /// Required field validation /// public static readonly string Required = nameof(Required); + + /// + /// Required field validation + /// + public static readonly string Custom = nameof(Custom); } }