The following validation scripts are included in the Motor Loaner System sample application that you can refer to when writing your own validation scripts:
- About the Associates Entity Email Field Validation Script
- About the Vehicle Models Entity Year Field Sample Validation Script
- About the Vehicles Entity VIN Field Sample Validation Script
- About the Vehicles Entity DisposalMethod and DisposalDate Fields Sample Validation Script
- About the Vehicles Entity VehicleStatusID Field Sample Validation Script
About the Associates Entity Email Field Validation Script
This field includes a validation script that compares the value provided by a user against a regular expression to determine if the email address is in an acceptable format. Within the context of the sample application, this requirement is defined in BPR4: Enforce Email Address Format and the implementation is described in About the Data Quality Design for the Sample Application.
It follows this process:
- Uses a geRecord.GetValue("Email").ToString <> " " statement to retrieve the value of the Email field from the current record's GE and determine if the Email field is blank. If blank, no further validation is applied.
- If the Email value is not blank, the script defines a regular expression for an e-mail address format and matches the Email value from the record against the regular expression.
- If the Email value matches the regular expression, the value passes the validation test and the script calls oResult.Success = True.
- If the Email value does not match the regular expression, the value fails the validation test. The script calls oResult.Success = False and oResult.Message to display an error message to the user that the value entered is not a valid Email address.
- Note that the sample script also includes code to handle situations where the script fails for unexpected reasons.
The full text of the script appears below:
' DEMO VALIDATION SCRIPT FOR MOTOR LOANER VEHICLE TRACKING SYSTEM ' ---------------------------------------------------------------------------------------------------------------------- ' This script enforces the business rule that only allows for ' valid email addresses to be entered for Associates. ' 'Field Validation Scripts can access this record's properties via geRecord. 'The Results of a script are returned via oResult.Success = [True|False] ' 'This sample script uses .Net's RegularExpression class to compare 'the entered email address against a regular expression. Try 'Only validate the Email field if it is not blank since the Email field is not required If geRecord.GetValue("Email").ToString <> "" Then Dim emailRegEx As System.String = "^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$" Dim myRegEx As New System.Text.RegularExpressions.Regex(emailRegEx) Dim myMatch As System.Text.RegularExpressions.Match myMatch = myRegEx.Match(geRecord.GetValue("Email").ToString) If myMatch.Success Then oResult.Success = True Else 'email did not pass regular expression test oResult.Success = False oResult.Message = geRecord.GetValue("Email").ToString & _ " is not a valid email address. Please reenter the Associate's email." End If Else 'nothing to validate, return true oResult.Success = True End If Catch 'Validation Script Failed oResult.Success = False oResult.Message = "The Email Field Validation script failed unexpectedly. " & _ "Check the script for logical errors." End Try
The Sample Application also contains a validation script that performs the same function for the Email field in the Customers entity.
About the Vehicle Models Entity Year Field Sample Validation Script
This field's validation script enforces a business rule that only allows only vehicle models between 10 years old and 1 year greater than the current year to be added to the system. It compares the current year against the value entered in the record's Year field.
Within the context of the sample application, this requirement is defined in BPR7: Prevent Older Model Vehicles from Entering Fleet and the implementation is described in About the Data Quality Design for the Sample Application.
The full text of the script appears below:
' DEMO VALIDATION SCRIPT FOR MOTOR LOANER VEHICLE TRACKING SYSTEM ' ---------------------------------------------------------------------------------------------------------------------- ' This script enforces the business rule that only allows for ' vehicles to have the Year field between 10 years old to 1 year ' greater than current Dim iCurrentYear As Integer = System.DateTime.Now.Year Dim iEnteredValue As Integer = CInt(geRecord.GetValue("Year")) If iEnteredValue < iCurrentYear-10 Then ' Validation Failed: Entered Value is Less than the Lower Bound for this Field: Its greater than 10 years old. oResult.Success = False oResult.Message = "Year must be greater or equal to " & (iCurrentYear-10).tostring ElseIf iEnteredValue > iCurrentYear + 1 Then ' Validation Failed: Entered Value is Greater Than the Current Year + 1 Year oResult.Success = False oResult.Message = iEnteredValue.ToString & _ " is more than a year past the current year (" & _ iCurrentYear.ToString & ") and is not allowed." Else ' Year is between the current year - 10 and the current year +1, ' so allow the validation process to continue by setting the ' result to True oResult.Success = True End If
About the Vehicles Entity VIN Field Sample Validation Script
This field includes a validation script that ensures that the value provided by a user is in the proper Vehicle Identification Number (VIN) format. Specifically, the script ensures that the value is 17 characters long does not contain these characters: o, O, i, I, u, or U.
Within the context of the sample application, this requirement is defined in BPR10: Enforce VIN Number Accuracy and the implementation is described in About the Data Quality Design for the Sample Application.
See Adding a Validation Script to a Field for a detailed example using this script.
About the Vehicles Entity DisposalMethod and DisposalDate Fields Sample Validation Script
These fields contain a validation script which ensures that when one field is set, the other field must also be set. (In other words, if a user enters a Date Disposed for a vehicle, then the user must also select a Disposal Method.)
Within the context of the sample application, this requirement is defined in BPR12: Track Vehicle Disposal and the implementation is described in About the Data Quality Design for the Sample Application.
The full text of the script for the DateDisposed field appears below. The field validation fails if the Disposal Method is set to a value other than N/A and the DateDisposed is either blank or NULL. Also, the script prevents a user from marking a vehicle as Disposed while it is Rented. (Note that since this validation appears for both fields, a user will receive a validation prompt with two duplicate messages if trying to dispose of a rented vehicle.)
If geRecord.Fields.Item("DateDisposed").IsDirty _ AndAlso geRecord.GetValue("VehicleStatus").ToString = "Rented" Then oResult.Success = False oResult.Message = "This Vehicle can not be disposed while its status is rented." Else If geRecord.GetValue("DisposalMethod").ToString <> "N/A" _ AndAlso (IsDBNull(geRecord.GetValue("DateDisposed")) _ OrElse geRecord.GetValue("DateDisposed").ToString = "") Then oResult.Success = False oResult.Message = "If this Vehicle has been disposed, both the method" & _ " and date of disposal must be specified." Else oResult.Success = True End If End If
The full text of the script for the DisposalMethod field appears below. The field validation fails if the DateDisposed is set (i.e., not blank or NULL) and the Disposal Method is set to N/A. Also, the script prevents a user from marking a vehicle as Disposed while it is Rented. (Note that since this validation appears for both fields, a user will receive a validation prompt with two duplicate messages if trying to dispose of a rented vehicle.)
If geRecord.Fields.Item("DisposalMethod").IsDirty _ AndAlso geRecord.GetValue("VehicleStatus").ToString = "Rented" Then oResult.Success = False oResult.Message = "This Vehicle can not be disposed while its status is rented." Else If Not IsDBNull(geRecord.GetValue("DateDisposed")) _ AndAlso geRecord.GetValue("DateDisposed").ToString <> "" _ AndAlso geRecord.GetValue("DisposalMethod").ToString = "N/A" Then oResult.Success = False oResult.Message = "If this Vehicle has been disposed, both the method" & _ " and date of disposal must be specified." Else oResult.Success = True End If End If
About the Vehicles Entity VehicleStatusID Field Sample Validation Script
This field's validation script automatically sets the vehicle's status automatically each time the record is saved. Within the context of the sample application, this requirement is defined in BPR11: Automatically Track Vehicle Status and Mileage and the implementation is described in About the Data Quality Design for the Sample Application.
The full text of the script appears below. This script queries other entities in the database to determine if outstanding Service Tickets or Rental Agreements exist for this vehicle to assist with setting the status appropriately. This is the most complicated of the validation scripts as it sets rather than merely checks the value of the VehicleStatusID field. It also includes Aptify's Exception Management functionality to capture and display any errors that occur during the validation update.
'Validation to enforce Business Rules for a Vehicle's Status 'When a Vehicle's Status is attempted to be changed, the following rules must be met for the status: '1. If a Vehicle's DateDisposed and DisposalMethod are filled in, then the Vehicle's Status is Discarded and cannot be used at all. '2. Else If a RentalAgreement exists for the VehicleID and the RentalDate is < Today and no ReturnDate exists then the Vehicle is currently Rented '3. Else If a ServiceTicket exists for the VehicleID and it is still outstanding (NOT (ServiceStatus = Completed AND ReleaseToFleet = 1) Then the Vehicle's Status is Servicing '4. Else the Vehicle is Available. Try Dim status As System.String Dim sSQL As System.String Dim count As System.Int32 Dim oDA As Aptify.Framework.DataServices.DataAction oDA = New Aptify.Framework.DataServices.DataAction(geRecord.UserCredentials) '1. Is this Vehicle Discarded? ' Assertion: Both the DateDisposed and DisposalMethod not N/A for the Vehicle to be 'Discarded' If Not IsDBNull(geRecord.GetValue("DateDisposed")) _ AndAlso geRecord.GetValue("DateDisposed").ToString <> "" _ AndAlso geRecord.GetValue("DisposalMethod").ToString <> "N/A" Then 'This Vehicle is Discarded status = "Discarded" Else '2. Is this Vehicle currently Rented? ' Assertion: If CheckInClerkID is filled in then the rental has been Checked-In sSQL = "SELECT count " & _ "FROM vwRentalAgreements " & _ "WHERE RentalDate < GetDate() " & _ "AND CheckInClerkID IS NULL " & _ "AND VehicleID = " & geRecord.GetValue("ID").ToString count = CInt(oDA.ExecuteScalar(sSQL)) If Not IsDBNull(count) And count > 0 Then 'This Vehicle is rented status = "Rented" Else '3. Is this vehicle being Serviced? sSQL = "SELECT count " & _ "FROM vwServiceTickets " & _ "WHERE NOT (ServiceStatus = 'Completed' " & _ "AND ReleaseToFleet = 1 ) " & _ "AND VehicleID = " & geRecord.GetValue("ID").ToString count = CInt(oDA.ExecuteScalar(sSQL)) If Not IsDBNull(count) And count > 0 Then 'This Vehicle is rented status = "Servicing" Else '4. If none of the above conditions are true, then this vehicle is Available status = "Available" End If End If End If 'Force the Vehicle's Status 'The StatusID will need to be queried from the VehicleStatuses entity sSQL = "SELECT ID " & _ "FROM vwVehicleStatuses " & _ "WHERE Name = '" & status & "'" Dim statusID As System.Int32 = CInt(oDA.ExecuteScalar(sSQL)) If Not IsDBNull(statusID) AndAlso statusID > 0 Then geRecord.SetValue("VehicleStatusID", statusID) oResult.Success = True Else oResult.Success = False oResult.Message = "The Vehicle Status script failed unexpectedly. " & _ " The query for " & status & " returned an ID of " & statusID & _ "Check the script for logical errors." End If Catch ex As System.Exception ' Validation Script Failed Aptify.Framework.ExceptionManagement.ExceptionManager.Publish(ex) oResult.Success = False oResult.Message = "The Vehicle Status script failed unexpectedly. " & _ "See the Error Log for more details." End Try
Comments
Please sign in to leave a comment.