Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

345 form validation #356

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions FMS.Domain/Dto/Facility/FacilityCreateDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public class FacilityCreateDto
[Display(Name = "Has Electronic Records")]
public bool HasERecord { get; set; }

[Display(Name = "Is Retained Onsite")]
public bool IsRetained { get; set; }

public void TrimAll()
{
FacilityNumber = FacilityNumber?.Trim();
Expand Down
125 changes: 125 additions & 0 deletions FMS/Helpers/FormValidationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using FMS.Domain.Dto;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Text.RegularExpressions;

namespace FMS.Helpers
{
public static class FormValidationHelper
{
public static ModelErrorCollection ValidateFacilityEditForm(FacilityEditDto facility)
{
ModelErrorCollection errCol = [];
DateOnly minDate = new DateOnly(1990, 1, 1);
DateOnly maxDate = DateOnly.FromDateTime(DateTime.Today);
string rnPattern = @"^\bRN\d{4}$";
string hsiPattern = @"^\d{5}$";
Regex rnRegex = new(rnPattern);
Regex hsiRegex = new(hsiPattern);

// Make sure GeoCoordinates are withing the State of Georgia or both Zero
GeoCoordHelper.CoordinateValidation EnumVal = GeoCoordHelper.ValidateCoordinates(facility.Latitude, facility.Longitude);
string ValidationString = GeoCoordHelper.GetDescription(EnumVal);

if (EnumVal != GeoCoordHelper.CoordinateValidation.Valid)
{
if (EnumVal == GeoCoordHelper.CoordinateValidation.LongNotInGeorgia)
{
errCol.Add(new ModelError(string.Concat("Facility.Longitude", "^", ValidationString)));
}
else
{
errCol.Add(new ModelError(string.Concat("Facility.Latitude", "^", ValidationString)));
}
}

// Check all things related to Release Notifications
if (facility.FacilityTypeName == "RN")
{
// Check Date Received
if ( facility.RNDateReceived is null)
{
errCol.Add(new ModelError(string.Concat("Facility.RNDateReceived", "^", "Date Received must be entered.")));
}
else if (facility.RNDateReceived > maxDate)
{
errCol.Add(new ModelError(string.Concat("Facility.RNDateReceived", "^", "Date must not be beyond today.")));
}
else if (facility.RNDateReceived < minDate)
{
errCol.Add(new ModelError(string.Concat("Facility.RNDateReceived", "^", "Date must not be before 1/1/1990.")));
}
// Check Facility Number
if (!rnRegex.IsMatch(facility.FacilityNumber))
{
errCol.Add(new ModelError(string.Concat("Facility.FacilityNumber", "^", "Facility Number must be in the form 'RNdddd'")));
}
// Check HSI Number
if (!facility.HSInumber.IsNullOrEmpty() && !hsiRegex.IsMatch(facility.HSInumber))
{
errCol.Add(new ModelError(string.Concat("Facility.HSInumber", "^", "HSI Number must be 5 digits Only.")));
}
}

return errCol;
}

public static ModelErrorCollection ValidateFacilityAddForm(FacilityCreateDto facility)
{
ModelErrorCollection errCol = [];
DateOnly minDate = new DateOnly(1990, 1, 1);
DateOnly maxDate = DateOnly.FromDateTime(DateTime.Today);
string rnPattern = @"^\bRN\d{4}$";
string hsiPattern = @"^\d{5}$";
Regex rnRegex = new(rnPattern);
Regex hsiRegex = new(hsiPattern);

// Make sure GeoCoordinates are withing the State of Georgia or both Zero
GeoCoordHelper.CoordinateValidation EnumVal = GeoCoordHelper.ValidateCoordinates(facility.Latitude, facility.Longitude);
string ValidationString = GeoCoordHelper.GetDescription(EnumVal);

if (EnumVal != GeoCoordHelper.CoordinateValidation.Valid)
{
if (EnumVal == GeoCoordHelper.CoordinateValidation.LongNotInGeorgia)
{
errCol.Add(new ModelError(string.Concat("Facility.Longitude", "^", ValidationString)));
}
else
{
errCol.Add(new ModelError(string.Concat("Facility.Latitude", "^", ValidationString)));
}
}

// Check all things related to Release Notifications
if (facility.FacilityTypeName == "RN")
{
// Check Date Received
if (facility.RNDateReceived is null)
{
errCol.Add(new ModelError(string.Concat("Facility.RNDateReceived", "^", "Date Received must be entered.")));
}
else if (facility.RNDateReceived > maxDate)
{
errCol.Add(new ModelError(string.Concat("Facility.RNDateReceived", "^", "Date must not be beyond today.")));
}
else if (facility.RNDateReceived < minDate)
{
errCol.Add(new ModelError(string.Concat("Facility.RNDateReceived", "^", "Date must not be before 1/1/1990.")));
}
// Check Facility Number
if (!rnRegex.IsMatch(facility.FacilityNumber))
{
errCol.Add(new ModelError(string.Concat("Facility.FacilityNumber", "^", "Facility Number must be in the form 'RNdddd'")));
}
// Check HSI Number
if (!facility.HSInumber.IsNullOrEmpty() && !hsiRegex.IsMatch(facility.HSInumber))
{
errCol.Add(new ModelError(string.Concat("Facility.HSInumber", "^", "HSI Number must be 5 digits Only.")));
}
}

return errCol;
}
}
}
5 changes: 5 additions & 0 deletions FMS/Pages/Facilities/Add.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
<input asp-for="Facility.HasERecord" />
<label asp-for="Facility.HasERecord" class="form-check-label"></label>
</div>
<div class="form-check">
<input asp-for="Facility.IsRetained" />
<label asp-for="Facility.IsRetained" class="form-check-label"></label>
</div>
</div>
<div class="col">
<label asp-for="Facility.Comments"></label>
Expand Down Expand Up @@ -171,6 +175,7 @@
<div class="col-md-3">
<label asp-for="Facility.HSInumber"></label>
<input asp-for="Facility.HSInumber" class="form-control" />
<span asp-validation-for="Facility.HSInumber" class="text-danger"></span>
</div>
</div>
<div class="mb-3 row">
Expand Down
45 changes: 25 additions & 20 deletions FMS/Pages/Facilities/Add.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FMS.Domain.Data;
using FMS.Domain.Dto;
using FMS.Domain.Entities.Users;
using FMS.Domain.Repositories;
using FMS.Helpers;
using FMS.Platform.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

Expand Down Expand Up @@ -70,26 +71,17 @@ public async Task<IActionResult> OnPostAsync()

Facility.TrimAll();

// Make sure Release Notifications have a "Date Received"
if (Facility.FacilityTypeName == "RN" && Facility.RNDateReceived is null)
{
ModelState.AddModelError("Facility.RNDateReceived", "Date Received must be entered.");
}

// Make sure GeoCoordinates are withing the State of Georgia or both Zero
GeoCoordHelper.CoordinateValidation EnumVal = GeoCoordHelper.ValidateCoordinates(Facility.Latitude, Facility.Longitude);
string ValidationString = GeoCoordHelper.GetDescription(EnumVal);

if (EnumVal != GeoCoordHelper.CoordinateValidation.Valid)
{
if (EnumVal == GeoCoordHelper.CoordinateValidation.LongNotInGeorgia)
{
ModelState.AddModelError("Facility.Longitude", ValidationString);
}
else
// Validate User input based on Business Logic
// Populate FacilityTypeName to use for User Input validity
Facility.FacilityTypeName = await _repositoryType.GetFacilityTypeNameAsync(Facility.FacilityTypeId);
ModelErrorCollection errors = FormValidationHelper.ValidateFacilityAddForm(Facility);
if (errors.Count > 0)
{
foreach (ModelError error in errors)
{
ModelState.AddModelError("Facility.Latitude", ValidationString);
}
string[] errMsg = error.ErrorMessage.Split("^");
ModelState.AddModelError(errMsg[0].ToString(), errMsg[1].ToString());
}
}

// If File Label is provided, make sure it exists
Expand Down Expand Up @@ -157,6 +149,19 @@ public async Task<IActionResult> OnPostConfirmAsync()
}
Facility.TrimAll();

// Validate User input based on Business Logic
// Populate FacilityTypeName to use for User Input validity
Facility.FacilityTypeName = await _repositoryType.GetFacilityTypeNameAsync(Facility.FacilityTypeId);
ModelErrorCollection errors = FormValidationHelper.ValidateFacilityAddForm(Facility);
if (errors.Count > 0)
{
foreach (ModelError error in errors)
{
string[] errMsg = error.ErrorMessage.Split("^");
ModelState.AddModelError(errMsg[0].ToString(), errMsg[1].ToString());
}
}

// If File Label is provided, make sure it exists
if (!string.IsNullOrWhiteSpace(Facility.FileLabel) && Facility.FileLabel != "none" &&
!await _repository.FileLabelExists(Facility.FileLabel))
Expand Down
8 changes: 7 additions & 1 deletion FMS/Pages/Facilities/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<dd class="col-sm-7">
@Html.DisplayFor(model => model.FacilityDetail.Cabinets, "Cabinets", new { Model.FacilityDetail.IsRetained })
</dd>
<dt class="col-sm-5">
@Html.DisplayNameFor(model => model.FacilityDetail.IsRetained)
</dt>
<dd class="col-sm-7">
@Html.DisplayFor(model => model.FacilityDetail.IsRetained)
</dd>
<dt class="col-sm-5">
@Html.DisplayNameFor(model => model.FacilityDetail.HasERecord)
</dt>
Expand Down Expand Up @@ -224,7 +230,7 @@
}
@if (!string.IsNullOrEmpty(Model.RNHSIFolderLink))
{
<a class="btn btn-sm btn-primary" target="_blank" [email protected]>RN HSI Folder</a>
<a class="btn btn-sm btn-primary" target="_blank" [email protected]>HSI Folder</a>
}
</div>

Expand Down
2 changes: 1 addition & 1 deletion FMS/Pages/Facilities/Details.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task<IActionResult> OnGetAsync(Guid? id, Guid? hr)

if (FacilityDetail.FacilityType.Name == "RN")
{
if (FacilityDetail.DeterminationLetterDate.HasValue)
if (FacilityDetail.DeterminationLetterDate.HasValue && string.IsNullOrEmpty(FacilityDetail.HSInumber))
{
NotificationFolderLink = UrlHelper.GetNotificationFolderLink(FacilityDetail.FacilityNumber);
}
Expand Down
5 changes: 5 additions & 0 deletions FMS/Pages/Facilities/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
<input asp-for="Facility.HasERecord" />
<label asp-for="Facility.HasERecord" class="form-check-label"></label>
</div>
<div class="form-check">
<input asp-for="Facility.IsRetained" />
<label asp-for="Facility.IsRetained" class="form-check-label"></label>
</div>
</div>
<div class="col">
<label asp-for="Facility.Comments"></label>
Expand Down Expand Up @@ -171,6 +175,7 @@
<div class="col-md-3">
<label asp-for="Facility.HSInumber"></label>
<input asp-for="Facility.HSInumber" class="form-control" />
<span asp-validation-for="Facility.HSInumber" class="text-danger"></span>
</div>
</div>
<div class="mb-3 row">
Expand Down
Loading
Loading