/// <summary> /// Does 2nd-level validation /// </summary> /// <param name="viewModel"></param> /// <returns></returns> private async Task ValidateAsync(GroupEditorViewModel viewModel) { if (viewModel.Action == ActionSave && ModelState.IsValid) { var dto = CreateSaveDto(viewModel); var validationEnvelope = await _groupWriteService.ValidateAsync(dto, User); validationEnvelope.Errors.ForEach(x => ModelState.AddModelError(x.Fields ?? string.Empty, x.GetMessage())); viewModel.SetWarnings(validationEnvelope); ModelState.Remove(nameof(viewModel.ProcessedWarnings)); } }
/// <summary> /// Does 2nd-level validation /// </summary> /// <param name="viewModel"></param> /// <returns></returns> private async Task ValidateAsync(GroupEditorViewModel viewModel) { if ((viewModel.Action == ActionSave || viewModel.Action == ActionDetails || viewModel.Action.StartsWith(ActionLinkedEstablishmentRemove) || viewModel.Action == ActionLinkedEstablishmentSearch || viewModel.Action == ActionLinkedEstablishmentAdd || viewModel.Action.StartsWith(ActionLinkedEstablishmentEdit) || viewModel.Action == ActionLinkedEstablishmentCancelEdit ) && ModelState.IsValid) { var dto = CreateSaveDto(viewModel); var validationEnvelope = await _groupWriteService.ValidateAsync(dto, User); if (viewModel.Action.StartsWith(ActionLinkedEstablishmentRemove) || viewModel.Action == ActionLinkedEstablishmentSearch || viewModel.Action == ActionLinkedEstablishmentAdd || viewModel.Action.StartsWith(ActionLinkedEstablishmentEdit) || viewModel.Action == ActionLinkedEstablishmentCancelEdit) { // ignore the message about the number of establishments in the group, as per JS behaviour for (var i = 0; i < validationEnvelope.Errors.Count; i++) { if (validationEnvelope.Errors[i].Code.Equals("error.validation.link.cc.one.linked.school")) { validationEnvelope.Errors.RemoveAt(i); } } } if (viewModel.Action == ActionSave || viewModel.Action == ActionDetails) { // we want to rebuild the screen once the removal has completed, so set the viewstate back to default viewModel.ClearWarnings(); } if (viewModel.Action.StartsWith(ActionLinkedEstablishmentRemove) || viewModel.Action == ActionLinkedEstablishmentCancelEdit) { // we want to rebuild the screen once the removal has completed, so set the viewstate back to default viewModel.ClearWarnings(); viewModel.ProcessedWarnings = false; } validationEnvelope.Errors.ForEach(x => ModelState.AddModelError(x.Fields?.Replace("Unmapped field: group.closedDate", nameof(viewModel.ClosedDate)) ?? string.Empty, x.GetMessage())); viewModel.SetWarnings(validationEnvelope); ModelState.Remove(nameof(viewModel.ProcessedWarnings)); } }