diff --git a/LearningHub.Nhs.WebUI/Controllers/AccountController.cs b/LearningHub.Nhs.WebUI/Controllers/AccountController.cs index 37813a17c..bdd0cf317 100644 --- a/LearningHub.Nhs.WebUI/Controllers/AccountController.cs +++ b/LearningHub.Nhs.WebUI/Controllers/AccountController.cs @@ -897,7 +897,7 @@ public async Task CreateAccountWorkStartDate(AccountCreationViewM } await this.multiPageFormService.SetMultiPageFormData(accountCreation, MultiPageFormDataFeature.AddRegistrationPrompt, this.TempData); - var dateVM = accountCreation.StartDate.HasValue ? new AccountCreationDateViewModel() { Day = accountCreation.StartDate.Value.Day, Month = accountCreation.StartDate.GetValueOrDefault().Month, Year = accountCreation.StartDate.Value.Year, FilterText = accountCreationViewModel.FilterText, ReturnToConfirmation = accountCreationViewModel.ReturnToConfirmation } : new AccountCreationDateViewModel() { FilterText = accountCreationViewModel.FilterText, ReturnToConfirmation = accountCreationViewModel.ReturnToConfirmation }; + var dateVM = accountCreation.StartDate.HasValue ? new AccountCreationDateViewModel() { Day = accountCreation.StartDate.HasValue ? accountCreation.StartDate.Value.Day.ToString() : string.Empty, Month = accountCreation.StartDate.HasValue ? accountCreation.StartDate.GetValueOrDefault().Month.ToString() : string.Empty, Year = accountCreation.StartDate.HasValue ? accountCreation.StartDate.Value.Year.ToString() : string.Empty, FilterText = accountCreationViewModel.FilterText, ReturnToConfirmation = accountCreationViewModel.ReturnToConfirmation } : new AccountCreationDateViewModel() { FilterText = accountCreationViewModel.FilterText, ReturnToConfirmation = accountCreationViewModel.ReturnToConfirmation }; if (!string.IsNullOrWhiteSpace(accountCreationViewModel.PrimarySpecialtyId) && string.IsNullOrWhiteSpace(accountCreationViewModel.FilterText)) { var specialty = this.specialtyService.GetSpecialtiesAsync().Result.FirstOrDefault(x => x.Id == specialtyId); @@ -959,7 +959,7 @@ public async Task CreateAccountStartDate(bool? returnToConfirmati } } - var dateVM = accountCreation.StartDate.HasValue ? new AccountCreationDateViewModel() { Day = accountCreation.StartDate.Value.Day, Month = accountCreation.StartDate.GetValueOrDefault().Month, Year = accountCreation.StartDate.Value.Year, ReturnToConfirmation = returnToConfirmation } : new AccountCreationDateViewModel() { ReturnToConfirmation = returnToConfirmation }; + var dateVM = accountCreation.StartDate.HasValue ? new AccountCreationDateViewModel() { Day = accountCreation.StartDate.HasValue ? accountCreation.StartDate.Value.Day.ToString() : string.Empty, Month = accountCreation.StartDate.HasValue ? accountCreation.StartDate.GetValueOrDefault().Month.ToString() : string.Empty, Year = accountCreation.StartDate.HasValue ? accountCreation.StartDate.Value.Year.ToString() : string.Empty, ReturnToConfirmation = returnToConfirmation } : new AccountCreationDateViewModel() { ReturnToConfirmation = returnToConfirmation }; return this.View("CreateAccountWorkStartDate", dateVM); } diff --git a/LearningHub.Nhs.WebUI/Models/Account/AccountCreationDateViewModel.cs b/LearningHub.Nhs.WebUI/Models/Account/AccountCreationDateViewModel.cs index c17fad8a9..68727eb35 100644 --- a/LearningHub.Nhs.WebUI/Models/Account/AccountCreationDateViewModel.cs +++ b/LearningHub.Nhs.WebUI/Models/Account/AccountCreationDateViewModel.cs @@ -13,17 +13,32 @@ public class AccountCreationDateViewModel : AccountCreationViewModel, IValidatab /// /// Gets or sets the Day. /// - public int? Day { get; set; } + public string Day { get; set; } + + /// + /// Gets or sets the Day Field. + /// + public int? DayInput { get; set; } /// /// Gets or sets the Country. /// - public int? Month { get; set; } + public string Month { get; set; } + + /// + /// Gets or sets the Month input. + /// + public int? MonthInput { get; set; } /// /// Gets or sets the Year. /// - public int? Year { get; set; } + public string Year { get; set; } + + /// + /// Gets or sets YearInput. + /// + public int? YearInput { get; set; } /// /// Gets or sets the GetDate. @@ -31,14 +46,46 @@ public class AccountCreationDateViewModel : AccountCreationViewModel, IValidatab /// DateTime. public DateTime? GetDate() { - return (this.Day.HasValue && this.Month.HasValue && this.Year.HasValue) ? new DateTime(this.Year!.Value, this.Month!.Value, this.Day!.Value) : (DateTime?)null; + return (this.DayInput.HasValue && this.MonthInput.HasValue && this.YearInput.HasValue) ? new DateTime(this.YearInput!.Value, this.MonthInput!.Value, this.DayInput!.Value) : (DateTime?)null; } /// public IEnumerable Validate(ValidationContext validationContext) { - return DateValidator.ValidateDate(this.Day, this.Month, this.Year, "valid start date") - .ToValidationResultList(nameof(this.Day), nameof(this.Month), nameof(this.Year)); + var validationResults = new List(); + int parsedDay = 0; + int parsedMonth = 0; + int parsedYear = 0; + + if (!string.IsNullOrWhiteSpace(this.Day) && !int.TryParse(this.Day, out parsedDay)) + { + validationResults.Add(new ValidationResult( + $"The value '{this.Day}' is not valid for Day.", new[] { nameof(this.Day) })); + } + + if (!string.IsNullOrWhiteSpace(this.Month) && !int.TryParse(this.Month, out parsedMonth)) + { + validationResults.Add(new ValidationResult( + $"The value '{this.Month}' is not valid for Month.", new[] { nameof(this.Month) })); + } + + if (!string.IsNullOrWhiteSpace(this.Year) && !int.TryParse(this.Year, out parsedYear)) + { + validationResults.Add(new ValidationResult( + $"The value '{this.Year}' is not valid for Year.", new[] { nameof(this.Year) })); + } + + if (validationResults.Count > 0) + { + return validationResults; + } + + this.DayInput = parsedDay; + this.MonthInput = parsedMonth; + this.YearInput = parsedYear; + + return DateValidator.ValidateDate(this.DayInput, this.MonthInput, this.YearInput, "valid start date") + .ToValidationResultList(nameof(this.Day), nameof(this.Month), nameof(this.Year)); } } }