Sanity Check Requested!
I've been writing code way too long for the past several days and just came
across this bit of logic I wrote several weeks ago. Suddenly something seems
wrong with it and I'm not sure what. I just would appreciate someone to look
over my shoulder (so to speek) and let me know if you think it's okay as is.
The context (of the code snippet below) is this: I dynamically add controls
and validators for those controls to a form at runtime. During PostBack the
following code executes - with the intent to prevent further server-side
processing if any of the controls do not pass validation (in cases where
client-side validation somehow was bypassed). Here's the snippet - my
paranoia is described below the snippet:
if ( ! Page.IsValid) {
if (Page.Validators.Count > 0) {
string strTemp = "";
foreach(System.Web.UI.IValidator currentValidator in Page.Validators) {
if ( ! currentValidator.IsValid) {
strTemp += currentValidator.ErrorMessage + "<br>";
}
}
if (strTemp.Length > 0) {
// Present the validator messages to the user AND prevent further
server-side processing
// ...
}
}
}
That's it. My paranoia is that it looks *possibly* wrong to test for
Page.IsValid first. Shouldn't I first loop through the Page.Validators
collection and trigger validation on each control? Speaking of which - does
[if ( ! currentValidator.IsValid) {] trigger the validation, or simply tell
me the result of any prior attempt to validate the data? If so, then how
would I actually trigger the validation - server side? Maybe not - maybe
everything is fine as is. See? I'm paraniod. (of course "just because you're
paranoid doesn't mean you're not being followed!" ...thus my post here).
This code seemed to make perfect sense a couple of weeks ago.
Thanks!