Sharing a Validator

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I have a form that contains a number of fields, all of which have
validators. There are two submit buttons, one to update a record and one to
add a record. Because the only difference in validation is to make sure
someone is not using a username that is already in use, I would like to be
able to use all the other validators for both the add and update buttons.
However, because as far as I know a validator can only be in one
validationgroup, that does not help me (although if they haven't already, I
think it would be a great improvement for the next version of the .NET
framework). Does anybody have any suggestions on a good way to solve my
problem (having 2 copies of each validator would work and be very simple,
but I would hope for a more efficient way)? Thanks.
 
A

ace_away

Why not use only one button? Change the text of the button from "Add" to
"Update" depending on the data that is populating your form.

Use a hidden textbox to keep the ID of the item. So if the textbox has a ID
>0 then it's an update, else it's an Add.

It's a little more complex than that, but it might get you started.
 
N

Nathan Sokalski

I would do that, but unfortunately the client wants to always see both the
Add and Update buttons. If I were designing what the page should look like,
I would probably do something similar to your suggestion. But unfortunately,
an unhappy client means an unhappy boss which means an unhappy paycheck. I
guess that's all part of the business, with people caring so much about the
look of the page.
 
M

Mark Rae [MVP]

[cross-posting removed]
a validator can only be in one validationgroup

This is one of the reasons that I never go anywhere near the validation
controls...

They're fine for basic stuff, but simply aren't flexible enough for more
complex validation.

For this, I'd just have one JavaScript function which accepts a parameter to
indicate whether the record is to be inserted or updated...

<script type="text/javascript">
function validateForm(pstrMode)
{
// common validation
if (pstrMode == 'New')
{
// extra validation for inserts
}
}
</script>

<asp:Button ID="cmdAdd" runat="server" Text="Add" OnCommand="Save_Command"
CommandArgument="Add" OnClientClick="return validateForm('New');" />
<asp:Button ID="cmdEdit" runat="server" Text="Edit" OnCommand="Save_Command"
CommandArgument="Edit" OnClientClick="return validateForm('Edit');" />
 
D

Dave Bush

The custom validation control works fine for the oddball cases. NOT
using the validation controls because they don't always fill the need is
very short sited.

Since I don't know exactly what you are coding, I won't go so far as to
say that I could do anything you are doing the hard way (imo) with a
custom validation control. But, I'm pretty darn sure I could. Which
would save me time in the long run because I could use the regular
validation controls for the things that are common.

-----Original Message-----
From: Mark Rae [MVP] [mailto:[email protected]]
Posted At: Monday, November 05, 2007 5:51 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: Sharing a Validator
Subject: Re: Sharing a Validator


[cross-posting removed]
a validator can only be in one validationgroup

This is one of the reasons that I never go anywhere near the validation
controls...

They're fine for basic stuff, but simply aren't flexible enough for more

complex validation.

For this, I'd just have one JavaScript function which accepts a
parameter to
indicate whether the record is to be inserted or updated...

<script type="text/javascript">
function validateForm(pstrMode)
{
// common validation
if (pstrMode == 'New')
{
// extra validation for inserts
}
}
</script>

<asp:Button ID="cmdAdd" runat="server" Text="Add"
OnCommand="Save_Command"
CommandArgument="Add" OnClientClick="return validateForm('New');" />
<asp:Button ID="cmdEdit" runat="server" Text="Edit"
OnCommand="Save_Command"
CommandArgument="Edit" OnClientClick="return validateForm('Edit');" />
 
M

Mark Rae [MVP]

The custom validation control works fine for the oddball cases. NOT
using the validation controls because they don't always fill the need is
very short sited.

Not short sited (or even short-sighted) at all - there are many scenarios
which simply don't fit into the standard set of validation controls:
http://www.peterblum.com/VAM/ValMain.aspx

No doubt you could write your own JavaScript to fit into the Custom
Validator, but why bother when you can just write the precise JavaScript you
need...?
 
N

Nathan Sokalski

That would be great for situations in which you did not care about
client-side validation, but I do want client-side validation (sort of). The
validators that differ between the Add and Edit is how you make sure a
duplicate username is not being created (when adding, it cannot exist
period, but with editing it can exist, but only as the current username of
the record being edited). I have written Validators (true validators that
inherits from the BaseValidator class) that use AJAX to check this
information on the server, because I do not want my users to have to
postback when filling out any of the form fields.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Mark Rae said:
[cross-posting removed]
a validator can only be in one validationgroup

This is one of the reasons that I never go anywhere near the validation
controls...

They're fine for basic stuff, but simply aren't flexible enough for more
complex validation.

For this, I'd just have one JavaScript function which accepts a parameter
to indicate whether the record is to be inserted or updated...

<script type="text/javascript">
function validateForm(pstrMode)
{
// common validation
if (pstrMode == 'New')
{
// extra validation for inserts
}
}
</script>

<asp:Button ID="cmdAdd" runat="server" Text="Add" OnCommand="Save_Command"
CommandArgument="Add" OnClientClick="return validateForm('New');" />
<asp:Button ID="cmdEdit" runat="server" Text="Edit"
OnCommand="Save_Command" CommandArgument="Edit" OnClientClick="return
validateForm('Edit');" />
 
M

Mark Rae [MVP]

That would be great for situations in which you did not care about
client-side validation, but I do want client-side validation (sort of).
The validators that differ between the Add and Edit is how you make sure a
duplicate username is not being created (when adding, it cannot exist
period, but with editing it can exist, but only as the current username of
the record being edited). I have written Validators (true validators that
inherits from the BaseValidator class) that use AJAX to check this
information on the server, because I do not want my users to have to
postback when filling out any of the form fields.

Correct - that's how I do it too...

So what's the problem...?
 
P

PJ on Development

Hi, Nathan

The validators work regardless the button pressed, however, you can
define the ValidationGroup for the button and that button would
validate only with the specified validation group (or validators that
does not have the ValidationGroup specified)... well, at least
according to the docs -- I didn't make any test with it so far.

Regards,

Paulo Santos
http://pjondevelopment.50webs.com
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top