

Generating the error message
|
<%
If isPostBack() then
Else
End If
%>
|
|
Looking at this part.
|
|
We now look at the final part - generating a suitable error message to be sent to the client is there is an error. At this stage we have both
an Dictionary object containing error messages, and a comma separated list of errors codes (representing errors) that have occured. We use a simple
function to split this comma separated list into a VBScript array, and then iterate this array, finding the corresponding matching error code
and message in the master errors Dictionary object. We then write it to the screen in an appropriate format.
In our HTML page, we change what we had to include a couple of extra lines. Before we had:
<body>
<%
Call WriteFormHeader("Form1Validator")
and so forth. Now, we include a couple of extra lines, like so:
<body>
<%
If not isEmpty(errCode) then
Call WriteUserErrors(objErrors, errCode, _
"An error has occurred. Please review the following list and resubmit the form.")
End If
Call WriteFormHeader("Form1Validator")
This extra sub will only be called in the event that the page has been reloaded. However, if everything has gone OK (ie there were no errors
in server-side validation) then we would have been redirected to the next page - and this sub wouldn't be called. Thus the only time that this
sub would be called is if the page has been reloaded, and server side validation picked up errors.
The actual code used by WriteUserErrors is fairly simple:
<%
Sub WriteUserErrors( _
ByRef objErrors, _
ByVal errCode, _
ByVal errMessage _
)
On Error Resume Next
Const Proc = "WriteUserErrors"
Dim arrErrors
Dim i
arrErrors = Split(errCode, ",")
Response.Write( _
"<p><span class=""Bold"">" & errMessage & "</span></p>" & vbCrLf & _
"<ul>" & vbCrLf)
For i = 0 to UBound(arrErrors)
Response.Write("<li>" & objErrors(arrErrors(i)) & "</li>" & vbCrLf)
Next
Response.Write("</ul>")
If Err.Number <> 0 then
Call subWriteError(Err.Number, Proc, Err.Description)
End if
End Sub
%>
Back to Server Side validation | Putting it all together
Back to Code Listing
|