Sharing a Validator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    Sharing a Validator

    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.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • ace_away

    #2
    Re: Sharing a Validator

    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.



    "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
    news:eQCewiZHIH A.3940@TK2MSFTN GP05.phx.gbl...
    >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
    >validationgrou p, 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.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

    >

    Comment

    • Nathan Sokalski

      #3
      Re: Sharing a Validator

      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.
      --
      Nathan Sokalski
      njsokalski@hotm ail.com
      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


      "ace_away" <ace@away.comwr ote in message
      news:uSWs3h%23H IHA.5764@TK2MSF TNGP06.phx.gbl. ..
      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.
      >
      >
      >
      "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
      news:eQCewiZHIH A.3940@TK2MSFTN GP05.phx.gbl...
      >>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
      >>validationgro up, 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.
      >--
      >Nathan Sokalski
      >njsokalski@hotm ail.com
      >http://www.nathansokalski.com/
      >>
      >
      >

      Comment

      • Mark Rae [MVP]

        #4
        Re: Sharing a Validator

        "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
        news:eQCewiZHIH A.3940@TK2MSFTN GP05.phx.gbl...

        [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(ps trMode)
        {
        // 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('N ew');" />
        <asp:Button ID="cmdEdit" runat="server" Text="Edit" OnCommand="Save _Command"
        CommandArgument ="Edit" OnClientClick=" return validateForm('E dit');" />


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • Dave Bush

          #5
          Re: Sharing a Validator

          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:mark@mar kNOSPAMrae.net]
          Posted At: Monday, November 05, 2007 5:51 PM
          Posted To: microsoft.publi c.dotnet.framew ork.aspnet
          Conversation: Sharing a Validator
          Subject: Re: Sharing a Validator

          "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
          news:eQCewiZHIH A.3940@TK2MSFTN GP05.phx.gbl...

          [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(ps trMode)
          {
          // 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('N ew');" />
          <asp:Button ID="cmdEdit" runat="server" Text="Edit"
          OnCommand="Save _Command"
          CommandArgument ="Edit" OnClientClick=" return validateForm('E dit');" />


          --
          Mark Rae
          ASP.NET MVP


          Comment

          • Mark Rae [MVP]

            #6
            Re: Sharing a Validator

            "Dave Bush" <davembush@dmbc llc.comwrote in message
            news:0E91F9BC1F 98473596BB46B4A BC73EAC@OfficeV ista...
            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:


            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...?


            --
            Mark Rae
            ASP.NET MVP


            Comment

            • Nathan Sokalski

              #7
              Re: Sharing a Validator

              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
              njsokalski@hotm ail.com
              有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


              "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
              news:e9Hvo5$HIH A.2064@TK2MSFTN GP06.phx.gbl...
              "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
              news:eQCewiZHIH A.3940@TK2MSFTN GP05.phx.gbl...
              >
              [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(ps trMode)
              {
              // 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('N ew');" />
              <asp:Button ID="cmdEdit" runat="server" Text="Edit"
              OnCommand="Save _Command" CommandArgument ="Edit" OnClientClick=" return
              validateForm('E dit');" />
              >
              >
              --
              Mark Rae
              ASP.NET MVP
              http://www.markrae.net

              Comment

              • Mark Rae [MVP]

                #8
                Re: Sharing a Validator

                "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
                news:Op%23f7NBI IHA.1316@TK2MSF TNGP02.phx.gbl. ..
                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...?


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • PJ on Development

                  #9
                  Re: Sharing a Validator

                  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


                  On Nov 2, 6:38 pm, "Nathan Sokalski" <njsokal...@hot mail.comwrote:
                  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.
                  --
                  Nathan Sokalski
                  njsokal...@hotm ail.comhttp://www.nathansokal ski.com/

                  Comment

                  Working...