Reference no: EM132403048
Ms Web Tech Requirement -Model-Based Validation & Class Libraries
In this requirement, you will be using partial & metadata classes, validation &format annotations, custom annotations, the Validate method in a self-validating model, and a class library. You are going to validate records for the Patient table.
Setup
1. Continue with your XXPatients project ... or you can download A1Patients and start from there.
a. If starting from A1Patients, put your name and section in the footer, and activate Session variables in startup.cs
XXPatientController
1. Generate the XXPatientControllerwith full CRUD support and Views for the Patient model.
2. Reduce the Index listing to the fields shown in the example below.
a. Show patient's full name, not separate first and last names, and order the listing by this.
b. Model annotations (later) will fix the field headings
c. Date of birth and date of death are Nullable<DateTime> datatypes, so ToString() and [DisplayFormat(...)] won't work on then directly ... something for you to figure out when displaying on a view.
d. Only show DateOfDeath if the patient is deceased
3. In the Create view:
a. Replace the provinceCode drop-down with a textbox andadd a field-validation <span> to display any error messages for it. Use another textbox on the page for reference.
4. In the Edit view, display the province name in the drop-down, ordered by name.
5. Catch any exception that is thrown on Create or Edit, place its innermostmessage into ModelState, and allow processing to continue to the sad path, which shouldredisplay the user's data with the error.
6. For Delete, put the innermost exception's message into TempData and return to the Delete view.
7. If the insert, update or delete works, display a success message on theMemberIndex page via TempData.
a. Notice I have ValidationSummary showing All error messages, so even errors on hidden fields show.
XXClassLibrary
1. Add a .NETCore class library called XXClassLibrary ... as a separate project in the XXPatients solution or as a project in a separate solution.Remember that when you create classes here, make them and their methodspublic or they won't be available to other projects like your web site.
2. Add a public staticclass called XXValidations, with the following public static(class-level) methods:
a. Add a method called XXCapitalize that accepts a string and returns a string:
i. If the input string is null, return it as an empty string.
ii. Change the input string to lower case and remove leading & trailing spaces.
iii. Shift the first letter of every word in the string to upper case.
iv. Return the newly-capitalised string.
b. Add a method called XXExtractDigits that accepts a string and returns a string:
i. Null is possible, so don't blow up on it.
ii. Return a string containing all digits found in the input string.
c. Add a method called XXPostalCodeValidation that accepts a string and returns a Boolean:
i. Use this method in your partial class' Validate method to verify a Canadian postal code, once you've decided the address is in Canada.
ii. This can be upper or lower case, with or without a space, and the letters have limited values ... the first letter can only be one of 18 letters and the others one of 20.
iii. If the input string is null or an empty string, have the method return true, permitting the postal code to be optional. Just spaces should fail.
iv. If the string fails the edit, return false, else return true.
d. Add a method called XXPostalCodeFormat that accepts a string and returns a string:
i. This is run if the postal validation returns true, so you may get null ... deal with it.
ii. Add the middle space, if missing, to the input string and return it, shifted to upper case.
e. Add a method called XXZipCodeValidation that accepts a string by reference and returns a Boolean:
i. Use this method to verify & format a US zip code, when the address is in the states.
ii. If the input string was null or empty, make it an empty string and return true.
iii. Extract all digits from the input string.
iv. If it contained 5 digits, put them into the input string and return true.
v. If it contained 9 digits,format them as 12345-1234 into the input string and return true.
vi. Else, return false and leave the input field unchanged.
3. Add a reference to your class library to your XXPatientsweb project.
Patient Class
All Patient validation code must be centralised in a metadata class and a like-named partial class, except for Remote & custom annotation code. Do not modify the generated Patient model and do not modify or validate data in controller actions.
1. Create a MetadataClasses subfolder inside theModels folder.
a. Create an XXPatientMetadata class file inside this subfolder.
b. Modify its namespace to be the same as the member model
c. Copy all physical property declarations from the Patient model to your XXPatientMetadata class.
d. Add a separate publicpartial class called Patient (ie: same as the model) to this class file:
i. Apply the XXPatientMetadata class to it, using an annotation.
ii. Turn the Patient partial class into a self-validating model by implementing the IValidatableObject interface. Implement the interface, creating aValidate method.
iii. Replace the "throw" statement with "yieldreturnValidationResult.Success" ... if you leave the throw, it'll always abend ... if you just take it out, the project won't build.
2. Display annotations & edits:
a. Change the field display-names to thoseshown in the sample Index, Create &Edit pages above.
b. Trim all strings of leading & trailing spaces.
c. Non-blank firstName,lastNameand genderare required. [Required] only checks for null/empty.
d. Use your XXValidations.Capitalize method to capitalise firstName, lastName, address,cityand gender.
e. If provinceCode is not empty or null, forceitto upper case and ...
i. Validate it by fetching its record from the database ... error if not found
ii. If fetching the province code throws an exception, put its innermost message in a ModelState so it displays beside the input field.
iii. Retain the record you found ... its country code is required to validate postalCode
f. postalCode is conditionally optional but,if provided:
i. Produce an error if provinceCode is invalid/missing ... it's required to edit a postal/zip code
ii. If provinceCode indicates the patient is in Canada, verify that the first letter of the postalCode is correct for that province. If not, produce a pertinent error message for both fields.
iii. Validate and format the postal/zip code using the relevant method(s) in your XXValidations Class (error if provinceCode is invalid/missing).
g. OHIP is optional, but if provided, shift it to upper case and ensure it's in this pattern: 1234-123-123-XX.
h. homePhoneis optional, but if provided:
i. It must contain exactly 10 digits (discard punctuation and text like "emergency only").
ii. Reformat into dash notation: 519-748-5220
iii. Sorry about historical data ... we didn't have edits. They'll be fixed on their next visit.
i. dateOfBirth is optional but, if provided, cannot be in the future
i. If deceased is true, dateOfDeath is required.
ii. If deceased is false,dateOfDeath must be null.
iii. If dateOfDeath is provided, it cannot be in the future or before dateOfBirth (if it is provided)
j. genderis required and must be "M", "F" or "X". Add a field-validation <span> to display error messageson Create & Edit views.
Attachment:- Model-Based Validation.rar