diff --git a/DigitalLearningSolutions.Web/Controllers/SupervisorController/Supervisor.cs b/DigitalLearningSolutions.Web/Controllers/SupervisorController/Supervisor.cs index ec0e630563..06c2b6c612 100644 --- a/DigitalLearningSolutions.Web/Controllers/SupervisorController/Supervisor.cs +++ b/DigitalLearningSolutions.Web/Controllers/SupervisorController/Supervisor.cs @@ -97,9 +97,13 @@ public IActionResult AddSuperviseDelegate(MyStaffListViewModel model) var adminId = GetAdminID(); var centreId = GetCentreId(); var supervisorEmail = GetUserEmail(); - AddSupervisorDelegateAndReturnId(adminId, model.DelegateEmail ?? String.Empty, supervisorEmail, centreId); + + ModelState.Remove("Page"); if (ModelState.IsValid) + { + AddSupervisorDelegateAndReturnId(adminId, model.DelegateEmail ?? String.Empty, supervisorEmail, centreId); return RedirectToAction("MyStaffList", model.Page); + } else { ModelState.ClearErrorsForAllFieldsExcept("DelegateEmail"); @@ -121,19 +125,27 @@ public IActionResult AddMultipleSuperviseDelegates(AddMultipleSupervisorDelegate var adminId = GetAdminID(); var centreId = GetCentreId(); var supervisorEmail = GetUserEmail(); - var delegateEmailsList = NewlineSeparatedStringListHelper.SplitNewlineSeparatedList(model.DelegateEmails); - foreach (var delegateEmail in delegateEmailsList) + + if (ModelState.IsValid) { - if (delegateEmail.Length > 0) + var delegateEmailsList = NewlineSeparatedStringListHelper.SplitNewlineSeparatedList(model.DelegateEmails); + foreach (var delegateEmail in delegateEmailsList) { - if (RegexStringValidationHelper.IsValidEmail(delegateEmail)) + if (delegateEmail.Length > 0) { - AddSupervisorDelegateAndReturnId(adminId, delegateEmail, supervisorEmail, centreId); + if (RegexStringValidationHelper.IsValidEmail(delegateEmail)) + { + AddSupervisorDelegateAndReturnId(adminId, delegateEmail, supervisorEmail, centreId); + } } } + return RedirectToAction("MyStaffList"); + } + else + { + ModelState.ClearErrorsForAllFieldsExcept("DelegateEmails"); + return View("AddMultipleSupervisorDelegates", model); } - - return RedirectToAction("MyStaffList"); } private void AddSupervisorDelegateAndReturnId( diff --git a/DigitalLearningSolutions.Web/Helpers/CommonValidationErrorMessages.cs b/DigitalLearningSolutions.Web/Helpers/CommonValidationErrorMessages.cs index a40a140815..1cbf50c4de 100644 --- a/DigitalLearningSolutions.Web/Helpers/CommonValidationErrorMessages.cs +++ b/DigitalLearningSolutions.Web/Helpers/CommonValidationErrorMessages.cs @@ -9,6 +9,8 @@ public static class CommonValidationErrorMessages public const string TooLongEmail = "Email must be 255 characters or fewer"; public const string InvalidEmail = "Enter an email in the correct format, like name@example.com"; public const string WhitespaceInEmail = "Email must not contain any whitespace characters"; + public const string EmailsRegexWithNewLineSeparator = @"(([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*\r\n\s*|\s*$))*"; + public const string InvalidMultiLineEmail = "Enter an email in the correct format (like name@example.com). Note: Each email address should be on separate lines"; public const string PasswordRegex = @"(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Za-z]).*"; public const string PasswordInvalidCharacters = "Password must contain at least 1 letter, 1 number and 1 symbol"; diff --git a/DigitalLearningSolutions.Web/ViewModels/Supervisor/AddMultipleSupervisorDelegatesViewModel.cs b/DigitalLearningSolutions.Web/ViewModels/Supervisor/AddMultipleSupervisorDelegatesViewModel.cs index ecb9b6623f..d8082a3147 100644 --- a/DigitalLearningSolutions.Web/ViewModels/Supervisor/AddMultipleSupervisorDelegatesViewModel.cs +++ b/DigitalLearningSolutions.Web/ViewModels/Supervisor/AddMultipleSupervisorDelegatesViewModel.cs @@ -1,4 +1,7 @@ -namespace DigitalLearningSolutions.Web.ViewModels.Supervisor +using DigitalLearningSolutions.Web.Helpers; +using System.ComponentModel.DataAnnotations; + +namespace DigitalLearningSolutions.Web.ViewModels.Supervisor { public class AddMultipleSupervisorDelegatesViewModel { @@ -7,6 +10,9 @@ public AddMultipleSupervisorDelegatesViewModel(string? delegateEmails) { DelegateEmails = delegateEmails; } + + [Required(ErrorMessage = "Enter an email")] + [RegularExpression(CommonValidationErrorMessages.EmailsRegexWithNewLineSeparator, ErrorMessage = CommonValidationErrorMessages.InvalidMultiLineEmail)] public string? DelegateEmails { get; set; } } } diff --git a/DigitalLearningSolutions.Web/Views/Supervisor/AddMultipleSupervisorDelegates.cshtml b/DigitalLearningSolutions.Web/Views/Supervisor/AddMultipleSupervisorDelegates.cshtml index b6ab91d8e0..519688fdff 100644 --- a/DigitalLearningSolutions.Web/Views/Supervisor/AddMultipleSupervisorDelegates.cshtml +++ b/DigitalLearningSolutions.Web/Views/Supervisor/AddMultipleSupervisorDelegates.cshtml @@ -33,7 +33,7 @@