Quote:
Originally posted by Sam Hobbs
No, "Dialog Data Validation" is one way of validating the data, but it is not the only way. You are entitled to always use DDV for data validation and it is good for you to suggest it as a possibility, but you are insisting that your way is the only correct way.
I didn't say it is the only way. There are always different ways to do something. You can even do data validation in PreTranslateMessage(), for that matter. I just pointed out that DDV was the way MFC provides for data validation, and that DoDataExchange() is a virtual function designed specifically for that purpose - so why not use it? Validation in OnOK is what I often see beginners doing who haven't yet learnt about the DDX/DDV mechanism.
Quote:
Originally posted by Sam Hobbs
Is Wombat correct? Does DoDataExchange not validate for an empty string? If so, then DoDataExchange does nt do exactly what is asked for.
Yes, he is correct in that the default implementation of DoDataExchange() doesn't check for an empty string (actually, it does nothing at all). But that's not what I said. He's also correct in that among the many predefined DDV_ macros, there is none which checks for an empty string. The solution is to either provide your own DDV_NotEmpty or DDV_MinChars macro, or to simply code it in terms of IsEmpty(). However, the place to do this test is, by design, DoDataExchange(), and it will work regardless of whether the user clicks 'OK', 'Apply' or switches to another property page.
Quote:
Originally posted by Sam Hobbs
When overriding OnOK, a custom error message can be provided, and restoring the focus to the control is just one line of code.
Even if it was only one line of code (it is not, as you first have to find the offending control(s), set the focus, restore the selection etc.), that single line alone would still be one line too much. Let alone the several lines, message map entry etc. needed to implement OnOK. DoDataExchange(), in contrast, need not be added: It's already there in wizard-generated dialog code.
Quote:
Originally posted by Sam Hobbs
Whose estimate?
Mine, based on the many, many pieces of code in various projects I have seen which do dialog data validation in either of the ways.
Quote:
Originally posted by Sam Hobbs
I really think that not everyone agrees that DDV is the only solution.
Again, not the only solution. In coding, there's never one single possible solution to a problem. You can always leave the path provided by the designers of a framework and do things in more complicated and less obvious ways, circumventing ready-made mechanisms which are there exclusively to make your coding work easier. But I don't see a point in doing so unless you have a very good reason.
Quote:
Originally posted by Sam Hobbs
These are all typical consequences of making modifications to software. There are many things that can be overlooked if a person does not know what to do or if they simply overlook something. It happens.
But one of the points in software design is to try to minimize the probability of those problems arising - not to create them artificially.
Quote:
Originally posted by Sam Hobbs
However the modifications relevant here would not be required when converting to modeless;
And what about the 'Apply' button? You would have to perform the same test you added to OnOK() in OnApply() too.
Quote:
Originally posted by Wombat
I do not see any DDV routines that check for emptiness of string.
You're right, there are none. As said above, you have to code them yourself (exactly the way you did in the code you posted). My point was only (and still is) that the most obvious place to do this, according to the design of MFC, is DoDataExchange, not OnOK.
Quote:
Originally posted by Wombat
My point is, whether you do validation in OnOK() or DoDataExchange(), both achieve their purpose and its all up to individual preferences. I do prefer using DoDataExchange, but in this case where the validation is a simple one, I think its acceptable to do it in OnOK() also.
I thought I pointed out why OnOK() is not an acceptable alternative. Besides the problems mentioned regarding possible future extensions towards modeles dialogs or property pages, it also requires you to locate the offending control, set the focus, set the selection, and prevent the dialog from being closed. CDataExchange::Fail() already does all this for you. And IMO, any solution which requires rewriting code which is already there for you to be used is not an acceptable alternative.