Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. WPF ValidationErrors

WPF ValidationErrors

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpwpfcomhelpquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    With WPF, you can set the NotifyOnValidationError property of some elements to true and add an error handler, e.g. Validation.AddErrorHandler(textID, ItemError);. Starting from a vaild state, you type some nonsense into the textbox, and you receive the event with e.Action = ValidationErrorEventAction.Added. That's what we should expect. But what happens if you type more nonsense into the textbox? Well, you receive another vaildation error with e.Action = ValidationErrorEventAction.Added. Hm, OK, validation failed again, and the error might be different. But now comes the WTF: immediately afterwards, you receive another such event, but now with e.Action = ValidationErrorEventAction.Removed! Yes, "Removed". Everything OK now? Can't be true, there's garbage in the textbox, not valid value. I changed my validator to include the text of the textbox. And voila: in the "Added" event, the current text is shown;, in the "Removed" event, the previous text. By the way, in .Net 3.5 the behavior was the other way round (first remove the previous error, then add the new error), see e.g. validationerror-giving-strange-behavior[^]

    B L Richard DeemingR 3 Replies Last reply
    0
    • B Bernhard Hiller

      With WPF, you can set the NotifyOnValidationError property of some elements to true and add an error handler, e.g. Validation.AddErrorHandler(textID, ItemError);. Starting from a vaild state, you type some nonsense into the textbox, and you receive the event with e.Action = ValidationErrorEventAction.Added. That's what we should expect. But what happens if you type more nonsense into the textbox? Well, you receive another vaildation error with e.Action = ValidationErrorEventAction.Added. Hm, OK, validation failed again, and the error might be different. But now comes the WTF: immediately afterwards, you receive another such event, but now with e.Action = ValidationErrorEventAction.Removed! Yes, "Removed". Everything OK now? Can't be true, there's garbage in the textbox, not valid value. I changed my validator to include the text of the textbox. And voila: in the "Added" event, the current text is shown;, in the "Removed" event, the previous text. By the way, in .Net 3.5 the behavior was the other way round (first remove the previous error, then add the new error), see e.g. validationerror-giving-strange-behavior[^]

      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #2

      And that wasn't listed as a breaking change. Wow, M$, you seriously f***** that one up.

      What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

      1 Reply Last reply
      0
      • B Bernhard Hiller

        With WPF, you can set the NotifyOnValidationError property of some elements to true and add an error handler, e.g. Validation.AddErrorHandler(textID, ItemError);. Starting from a vaild state, you type some nonsense into the textbox, and you receive the event with e.Action = ValidationErrorEventAction.Added. That's what we should expect. But what happens if you type more nonsense into the textbox? Well, you receive another vaildation error with e.Action = ValidationErrorEventAction.Added. Hm, OK, validation failed again, and the error might be different. But now comes the WTF: immediately afterwards, you receive another such event, but now with e.Action = ValidationErrorEventAction.Removed! Yes, "Removed". Everything OK now? Can't be true, there's garbage in the textbox, not valid value. I changed my validator to include the text of the textbox. And voila: in the "Added" event, the current text is shown;, in the "Removed" event, the previous text. By the way, in .Net 3.5 the behavior was the other way round (first remove the previous error, then add the new error), see e.g. validationerror-giving-strange-behavior[^]

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        That's only one of the million reasons why nobody should validate user input. Ever! A developer's productivity is totally sabotaged when he's sent to the input validation pits. Not to talk about motivation! There should be a role for that: we already have test engineers, so why not? :doh: Well, there is not a specific role that I know of, but apparently there is a profoundly wise rule: GIGO[^]. If a user is stupid or mean enough to feed garbage into the software, then they totally deserve to be handed back some pile of cr@p. :D Just sayin'.

        Anything that could possibly go wrong in some moment, will definitely go wrong in the worst possible moment...
        In the worst way that could be possible!

        –Finagle's corollary to Murphy's Law (paraphrased).

        B L 2 Replies Last reply
        0
        • L Lost User

          That's only one of the million reasons why nobody should validate user input. Ever! A developer's productivity is totally sabotaged when he's sent to the input validation pits. Not to talk about motivation! There should be a role for that: we already have test engineers, so why not? :doh: Well, there is not a specific role that I know of, but apparently there is a profoundly wise rule: GIGO[^]. If a user is stupid or mean enough to feed garbage into the software, then they totally deserve to be handed back some pile of cr@p. :D Just sayin'.

          Anything that could possibly go wrong in some moment, will definitely go wrong in the worst possible moment...
          In the worst way that could be possible!

          –Finagle's corollary to Murphy's Law (paraphrased).

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          You are right. But I want to make the programs fool-proof when they are intended for my own use: I know my tyipng bheavior.

          L 1 Reply Last reply
          0
          • L Lost User

            That's only one of the million reasons why nobody should validate user input. Ever! A developer's productivity is totally sabotaged when he's sent to the input validation pits. Not to talk about motivation! There should be a role for that: we already have test engineers, so why not? :doh: Well, there is not a specific role that I know of, but apparently there is a profoundly wise rule: GIGO[^]. If a user is stupid or mean enough to feed garbage into the software, then they totally deserve to be handed back some pile of cr@p. :D Just sayin'.

            Anything that could possibly go wrong in some moment, will definitely go wrong in the worst possible moment...
            In the worst way that could be possible!

            –Finagle's corollary to Murphy's Law (paraphrased).

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I agree and don't. Input-validation is a nice thing, if it is generated; especially cool for banknumbers and social security-numbers, things where I tend to make mistakes. Most of the validations CAN be generated, so why aren't they? It is not something that one should blindly spend money on; in that case it will simply cost money, often without knowing the return-on-investment of the effort - how many errors did it actually prevent, and at what cost? No, it is more valuable to invest time and think about what errors you want to prevent. One of the things often omitted is the check for duplicate records - many places use identity-fields simply to not have to worry about a primary key-field.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

            1 Reply Last reply
            0
            • B Bernhard Hiller

              You are right. But I want to make the programs fool-proof when they are intended for my own use: I know my tyipng bheavior.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Tip; turn of your spellchecker. I've seen more errors in recent "spell checked" documents than in old unchecked versions. I think that the spellcheckers are causing them, instead of preventing.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

              B 1 Reply Last reply
              0
              • L Lost User

                Tip; turn of your spellchecker. I've seen more errors in recent "spell checked" documents than in old unchecked versions. I think that the spellcheckers are causing them, instead of preventing.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                B Offline
                B Offline
                Brisingr Aerowing
                wrote on last edited by
                #7

                Or the Autocowreck (AKA Autoincorrect)

                What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

                1 Reply Last reply
                0
                • B Bernhard Hiller

                  With WPF, you can set the NotifyOnValidationError property of some elements to true and add an error handler, e.g. Validation.AddErrorHandler(textID, ItemError);. Starting from a vaild state, you type some nonsense into the textbox, and you receive the event with e.Action = ValidationErrorEventAction.Added. That's what we should expect. But what happens if you type more nonsense into the textbox? Well, you receive another vaildation error with e.Action = ValidationErrorEventAction.Added. Hm, OK, validation failed again, and the error might be different. But now comes the WTF: immediately afterwards, you receive another such event, but now with e.Action = ValidationErrorEventAction.Removed! Yes, "Removed". Everything OK now? Can't be true, there's garbage in the textbox, not valid value. I changed my validator to include the text of the textbox. And voila: in the "Added" event, the current text is shown;, in the "Removed" event, the previous text. By the way, in .Net 3.5 the behavior was the other way round (first remove the previous error, then add the new error), see e.g. validationerror-giving-strange-behavior[^]

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  Looking at the comments[^], the change was made to avoid "a transient 'no error' state". :~


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  B 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Looking at the comments[^], the change was made to avoid "a transient 'no error' state". :~


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    B Offline
                    B Offline
                    Bernhard Hiller
                    wrote on last edited by
                    #9

                    That's a good point. Their reason is totally valid. But their solution is NOT. When an error has to be replaced by a different error, the system is in an invalid state when only one of the two steps of adding the new error and removing the old error has been completed. Two errors on the field are as wrong as no error on it. It's similar to transferring money from one bank account to another bank account: do not create or loose money by omitting one step. A solution could be a transaction. But I think the simplest solution here is similar to a PropertyChanged event. Get rid of the Added/Removed enum, and take two error properties: OldError and NewError. When you leave the valid state, NewError contains the error which is now communicated with Added, and OldError is null. When an error gets replaced by a different error, NewError contains the freshly incurred error (now with Added), and OldError the previously encountered error (now with Removed). When you return to a valid state, NewError is null, and OldError the previously encountered error (now with Removed). That's a better solution, isn't it?

                    Richard DeemingR 1 Reply Last reply
                    0
                    • B Bernhard Hiller

                      That's a good point. Their reason is totally valid. But their solution is NOT. When an error has to be replaced by a different error, the system is in an invalid state when only one of the two steps of adding the new error and removing the old error has been completed. Two errors on the field are as wrong as no error on it. It's similar to transferring money from one bank account to another bank account: do not create or loose money by omitting one step. A solution could be a transaction. But I think the simplest solution here is similar to a PropertyChanged event. Get rid of the Added/Removed enum, and take two error properties: OldError and NewError. When you leave the valid state, NewError contains the error which is now communicated with Added, and OldError is null. When an error gets replaced by a different error, NewError contains the freshly incurred error (now with Added), and OldError the previously encountered error (now with Removed). When you return to a valid state, NewError is null, and OldError the previously encountered error (now with Removed). That's a better solution, isn't it?

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      That would be a better solution. Now you just need to convince Microsoft to adopt it! :)


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups