Server Side Validation

F

Frankie

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!
 
S

S. Justin Gengo

Frankie,

Page validation gets triggered automatically on a postback. I have noticed
that if you are using a control's auto post back function (E.g. an auto post
back on a checkbox) that page validation isn't fired for that.

If a button click is trigerring your post back don't bother to call page
validation on your own. If it's an auto post back then fire page validation
manually with Page.Validate then you only need to check if the whole page is
valid or not, there is no need to check each validator yourself, that's what
Page.IsValid does.

So:

Page.Validate //only need to call this method if routine is running because
of a control's auto post back a button click would have already run
Page.Validate

if (Page.IsValid) {
//run your code here
}

If the page isn't valid the error message you've set on the validators when
they were created will be displayed automatically.

You've really recreated a lot of functionality that happens automatically...

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
Frankie said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top