Best Practices for Field Validation

This is an architectural question and since I'm relatively new to MFC I
hope I can get some advice from more knowledgable folks.

I have an app that I'm moving to WinCE (4.2) with MFC and there are a
number of dialogs--about 30--that each contain different fields. In
this app there are a number of different units in use--e.g., 5.08
metres, 185 PSI, 1.8 in, etc.--and each setting has its own limits.

For example, on a particular screen, field 1 can be set anywhere
between 1 - 100, and field 2 can be between 1 - 60. Each field on each
screen has this min and max.

I'm looking for an efficient and manageable way to perform field
validation without resorting to Massive Switch Statements(tm) inside my
OnOK handler.

What I had in mind was something like the following:
for (int i = IDC_FIELD1; i <= IDC_FIELDX; i++) {
value = aFields[i];
if ((value < GetMinValue(i)) || (value > GetMaxValue(i)) {
ShowValidationMessage(i);
break;
}
}

I'm operating under the assumption that control ID's across the entire
app are unique and that edit control ID's on a single screen are
sequential.

Any comments as to whether my ideas is good or why my idea sucks would
be greatly appreciated. I'd like to get some feedback before I
implement something like this application-wide.

TIA,
Dan.

0
dmccarty (45)
12/27/2004 9:35:40 PM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
571 Views

Similar Articles

[PageSpeed] 28

"Dan McCarty" <dmccarty@gmail.com> wrote in message
news:1104183340.462289.143500@f14g2000cwb.googlegroups.com...
> This is an architectural question and since I'm relatively new to MFC I
> hope I can get some advice from more knowledgable folks.
>
> I have an app that I'm moving to WinCE (4.2) with MFC and there are a
> number of dialogs--about 30--that each contain different fields. In
> this app there are a number of different units in use--e.g., 5.08
> metres, 185 PSI, 1.8 in, etc.--and each setting has its own limits.
>
> For example, on a particular screen, field 1 can be set anywhere
> between 1 - 100, and field 2 can be between 1 - 60. Each field on each
> screen has this min and max.
>
> I'm looking for an efficient and manageable way to perform field
> validation without resorting to Massive Switch Statements(tm) inside my
> OnOK handler.
>
> What I had in mind was something like the following:
> for (int i = IDC_FIELD1; i <= IDC_FIELDX; i++) {
> value = aFields[i];
> if ((value < GetMinValue(i)) || (value > GetMaxValue(i)) {
> ShowValidationMessage(i);
> break;
> }
> }
>
> I'm operating under the assumption that control ID's across the entire
> app are unique and that edit control ID's on a single screen are
> sequential.
>
> Any comments as to whether my ideas is good or why my idea sucks would
> be greatly appreciated. I'd like to get some feedback before I
> implement something like this application-wide.
>
> TIA,
> Dan.
>

The only thing that comes to mind is creating a CMap to map a control ID to
a pointer to a class that contains constraints, units, a validation
function, etc.

Something like (off the top of my head)
class EditValidation
{
    CString m_Units;
    long m_MinVal;
    long m_MaxVal;
    bool Validate(LPCTSTR TheText, CString &ErrorMsg);
};

CMap<int, int &, EditValidation *, EditValidation *&> ControlValidationMap;

for (int ControlID = IDC_FIELD; ControlID <= IDC_FIELDX; ++ControlID)
{
    void *ptr;
    ControlValidationMap.Lookup(ControlID, ptr);
    EditValidation *EV = (EditValidation *) ptr;
    CString value = aFields[ControlID];
    if (ptr && !ptr->Validate(TheText, ErrorMsg)
    {
        ShowValidationError(ControlID, ErrorMsg);
    }
}

This is very rough, but something along these lines might do the trick for
you.  Of course you will have to set up the ControlValidationMap.

You  might consider using class templates to handle different data types
(e.g. double instead of long).


0
billt61 (145)
12/28/2004 12:20:48 AM
>This is an architectural question and since I'm relatively new to MFC I
>hope I can get some advice from more knowledgable folks.
>
>I have an app that I'm moving to WinCE (4.2) with MFC and there are a
>number of dialogs--about 30--that each contain different fields. In
>this app there are a number of different units in use--e.g., 5.08
>metres, 185 PSI, 1.8 in, etc.--and each setting has its own limits.
>
>For example, on a particular screen, field 1 can be set anywhere
>between 1 - 100, and field 2 can be between 1 - 60. Each field on each
>screen has this min and max.
>
>I'm looking for an efficient and manageable way to perform field
>validation without resorting to Massive Switch Statements(tm) inside my
>OnOK handler.

Dan,

Are you aware of MFC's DDV facilities?

Dave
-- 
MVP VC++ FAQ: http://www.mvps.org/vcfaq
0
davidl7375 (2060)
12/28/2004 12:31:41 AM
This is exactly the kind of thing that I was looking for.  Thanks very
much.

-Dan.

0
dmccarty (45)
12/28/2004 9:47:52 PM
Hi Bill,
Using a CMap sounds like a good idea, but using MFC's built-in
DDX/DDV like David suggested sounds like an even better idea.
Thanks,
Dan.

0
dmccarty (45)
12/28/2004 9:49:13 PM
Reply:

Similar Artilces:

i cant display field list in pivot table
i cant display the field chooser in pivot table What version of Excel are you using? Do you see the field list for other pivot tables? Cesar wrote: > i cant display the field chooser in pivot table -- Debra Dalgleish Excel FAQ, Tips & Book List http://www.contextures.com/tiptech.html ...

number of fields exceeds 256 when importing from a dbf file
Hey evryone, I'm having a small problem with importing some old data from a FileMaker Pro database. According to what I found out in the groups archive the best bet is to convert it to a dbf file and then import it. However, I the number of fields in the dbf file exceeds 256, so the import wizard truncates the import at field number 256. I would now want to spread the data over two sheets. However, in the process of converting it to a dbf, quite a few of the field names get truncated and therefore have the same name, so I can't use SQL to retrieve them. My question is: what i...

XML schema validation #3
Hi, I have a XML file which I validate against a schema using the following: doc.Schemas.Add("", xsdSchema); doc.Validate(new System.Xml.Schema.ValidationEventHandler(ValidationErrorEvent)); This works fine, and I get the correct error when changing element names and structure. But when the XML to validate contains a different namespace than the one trying to validate I don't get an error, I can see why. So my question is how do I define the schema or my code to fail validation when a wrong NS is contained in the XML. Samples: <root><elements>value</elements>...

Disabling a customised field
hi how can i disable a customized field(textbox ) in mscrm form???? Thanks Just go to the form and remove that field from it - it only removes the field from the form not from the schema. >-----Original Message----- >hi > >how can i disable a customized field(textbox ) in mscrm form???? > > >Thanks > > > >. > hi , thanks for the reply. but i need to show my textbox as a readonly(the disabled sort of textbox) rather than removing it from the form. How can i achieve it in both in runtime and designtime ?? Thanks Sorry but MSCRM does not mak...

Best grid for data...
I have to use a grid to display data. The data is not from a database and hence I cannot use data bound grids. Now, there are so many grids that Iam totally confused which one to use. For now, I just have to display information and sort the rows in ascending/descending order when the user clicks on the column header. This can be achieved even by MS Flex grid. But Iam looking for a grid that is flexible and extensible in case there are new requirements like editing cell data. Can somebody help me find a good grid? Ofcourse Iam looking for freeware. thanks for your patience. T...

Invalid Value types in Lookup field
I am new at Access and don't really know what I am doing. I created two tables... 1. contact information 2. subcontractors used at job sites. I created a lookup field for the subcontractors so when I create reports or queries I can connect the subcontractors with their contact information. I set the field to text, but everytime I try to put the cursor in the field it comes up with an error message saying that the value isn't valid for the field. How do I get it to stop doing this? Your first problem was defining a "lookup field" -- I'm sure Microsoft was tryi...

Reading XML
I am reading the field names of Leads into a dataset - and only the first 13 or so show up. I import them to a string - then to a dataset. Do you think the string is the limiting factor? I am basically trying to mimic the Import Wizard for Leads and add a duplicate check function. If anyone knows where the code is for that wizard I would appreciate it as well!! THANKS! How are you getting the field names? Using SQL to get them would be much easier and considerably faster (the SDK is horrible slow compared to SQL calls, just don't use SQL to make changes, read only). I'm assumi...

SendObject doesn't find fields in querry
I have a form that has a button to run an "event Procedure" to preview a report, and to email the report as a pdf attachment. The codes I use are as follows: Private Sub emailfullpricesheet_Click() DoCmd.OpenReport "Full Price Sheet", acViewPreview, , , acWindowNormal DoCmd.SendObject acSendReport, "Full Price Sheet", acFormatPDF, , , , [DLRName], _ "Thank You for your business.", True End Sub In this code, I have the field for the dealer name (shown above as [DLRName]) where it would be placed in the subje...

Rule for the from field on outgoing mail ?
Hello I have some Exchange mailboxes which several users have added to their exchange accounts and have permission to send on behalf of. Now Id like their replies to mail in this inbox to wind up in the same inbox. One way seem to use an event sink like: http://www.ivasoft.biz/unisent.shtml Another would be creating rules. Now, when I start looking at the rules I find that you can't create rules using the from field on outgoing mail in Outlook 2003... This must be some kind of mistake, is there a workaround or a standalone Outlook rules editor which takes an exported rules file an...

Web-based Outlook bug: Cannot open mail when the subject field ends with a . (dot) character
From web-based Outlook, you cannot open any mail with the subject field ends with "." (dot) character. Outlook web will return HTTP 404: The page cannot be found. You might want to post this in an exchange group as Outlook Web Access is actually a function of Exchange, not Outlook. microsoft.public.exchange.admin is a good place to start. --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the (insert latest virus name here) virus, all mail sent to my personal account will be deleted without reading. After searching google.grou...

Best in Class Visual Reports for Mgmt
Hi, I am looking for some examples of 'Best in Class' visual reports for hi-level management use. Specifically, reports that demonstrate the project status in both financials (ex: EVA) and overall progress. I know there are some of the standard MS Projects reports, but my manager is looking for any additional reports that have favorable reviews. Thanks so much for your help! Download the free 30 day trial version of DecisionEdge. There's a lot of sample reports there that are all validated by industry. "shasta1987" wrote: > Hi, > > I...

How to check continuous form fields
I have a tabbed form, when I switch from one tab to another, it checks to make sure that none of the mandatory fields are missing. However the code that checks the records on tab #1 (which is the code below after the elseif), the code only checks the first record. I can not figure out how to modify the code to get it to look at other records on the form that may be present. Any suggestions on what modification I would need to make here would be helpful. thanks! If TabCtl4 = 1 Then Dim CkCtls As New Collection CkCtls.Add "DRNo" ...

Sorting by Text field of numbers
I am using Access 2003. I have created a database to help me files by "Lot#'s". Our lot numbers for one of our products is all over the place so I created the Lot # field as a text field. Ex: 02-01-01, 10000272925, 10-06-02-04-06 and 1503011 A1-02. Why does access put - 1000272925 before 10-06-02-03-05? Does this look right to anyone? My thinking is that "10" comes before "1000272925". What affect does the "-" have in sorting the numbers. Would I be better off to put a space instead of the "-"? Can anyone help me ...

i get a message too many fields defined on ACCESS
Trying to add columns, and it does not let me. What can I do Ibz�n wrote: > Trying to add columns, and it does not let me. > > What can I do I believe the limit is 255, but you may need to do a compact and repair (see top line menu) to free up previously deleted fields. If you are anywhere over about 30 or 40 fields, you likely need to revise your table design and "Normalize" it. -- Joseph Meehan Dia 's Muire duit ...

User Defined Field (String)
Does anyone know of a way I can force a user-defined field (I'm working specifically with User Defined 1 in Sales Trx Entry - a String(20) field) to force formatting at data entry time to US dollar (eg. $100.00) formatting? Either with VBA or Modifier? I would need to do this a couple of individual User Defined fields in the SOP User Defined fields entry window. Thanks in advance. Not a good way to do this - you could maybe use a vba event to format the data after the user enters it but vba isn't real happy typically of setting the current field when running its ...

calculated field from a form to table
Can you take a calculated field from a form and pull it in to a table?? You can but you should not. Instead, calculate the value each time you need it. Stored values are almost guaranteed to get out of synch sooner or later. HTH -- -Larry- -- "Darren" <Darren@discussions.microsoft.com> wrote in message news:4A519CB5-029B-46FF-80B5-22D6FEA0C7AB@microsoft.com... > Can you take a calculated field from a form and pull it in to a table?? Ok, makes sense. If i wanted to do it to get a snapshot and save that in the table, how do you do this? "Larry Daugherty" w...

Selected items from List Box sent to Form Field
When items are selected from a list box (frm!List52), they currently populate another list box (frm!List56) but I would like the selections made to also update a field (T_assigned) on the form. Currently frm! List56 is populated with first name and last name data - Columns 1 and 2 - I am trying to get the selected ID_NUMs from Column 0 into the T_assigned field. Any help or guidance would be greatly appreciated, thanks. ______________________________________________ Public Sub CopySelected(ByRef frm As Form) Dim ctlSource As Control Dim ctlDest As Control Dim strItems As String...

splitting of data elements
Not sure of the best way to approach this so I thought i'd seek help I have been asked to split a contact name column in a database to its corresponding parts Title Firstname / forename Surname Letters after name e.g. BSc and Joe, its not my database so I can do without the usual 1FN / bad designs lecture please, i've just been asked to do the work. Im not sure if its best done in dot net or SQL? Thanks Simon Simon Whale wrote: > Not sure of the best way to approach this so I thought i'd seek help > > I have been asked to split a contact na...

Omega Seamaster Aqua Terra Chronograph Large Mens Watch 2812.52.37, Best Wristwatch World
Omega Seamaster Aqua Terra Chronograph Large Mens Watch 2812.52.37, Best Wristwatch World Click Here To Website : http://www.watchebay.net/Omega-Seamaster-Aqua-Terra-Chronograph-Large-Mens-Watch-2812.52.37.html Wristwatch World: http://www.watchebay.net/ Omega Seamaster Aqua Terra Chronograph Large Mens Watch 2812.52.37 Information : Brand : Omega Watches ( http://www.watchebay.net/Omega-Watches.html ) Gender : Mens Code : Omega-2812.52.37 Also Called : 2812.52.37, Omega-2812.52.37, 2812-52-37, 2812/52/37, 28125237, 2812 Case Material : Stainless Steel ...

Business Portal 2.5 installation error "Unable to validate company information in your backoffice data."
hi, i got this message Unable to validate company information in your backoffice data upon installing Business Postal 2.5 Please help.. tnx Stephen, Thanks for using the newsgroups. The error you got generally means there is a data issue with either the DYNAMICS..SY01500 or DYNAMICS..DB_Upgrade tables. Verify that all companies listed in those tables are still valid in your environment. Thanks, Rob -- Rob Bernhardt Software Quality Assurance Microsoft Business Solutions This posting is provided "AS IS" with no warranties, and confers no rights. "Stephen Lee Glo...

Excel VBA
Hi Excel VBA advisers! I have a userform where there are two text boxes (drugdesc1 & drugdesc2) that I want to force 4 commas to be keyed into both fields. 4 commas are MANDATORY in each text box & are used to separate the type of input within each. ie; brand name, ingredients, strength, form, pack size. (FYI: Later on, I split these fields into 4 separate columns based on the comma delimiter.) When "cmdAdd" command button is clicked, I need to display a msgbox to advise user and disallow the add. I imagine that the code (repeated for each text box) that I would put in th...

Subform
I'm having a problem loading data in a subform. The fields in the subform and main form are not bound. I'm using MSAccess 2007 I put VBA code in the "On Load" event of the subform to get records from a table and populate the fields on the subform after the main form is opened from a previous form but I'm getting the message: " MSAccess can't find the form referenced in the VBA code" (referring to the subform, mysubform) . As a test, I put the code to populate the subform (including, as the first line of the code, "DoCmd...

Issue when combining fields
I have combined 3 fields (firstname, lastname, business) in a query to come up with a concatenated field called custname. The only problem is when one of the fields has no data it puts a space in where the data would have been inserted into the string. For instance, if neither firstname nor lastname have data, and business does, I end up with something like (space) Scott's. The problem with this is that if I want to sort alphabetically, anything with a space before it comes up first. Is there a way to fix this? Thanks! Trim(FirstName & " " & LastName ) If Fir...

formatting a date in a text field
I need to format a date that is in a text field - ex. 20081108 needs to be 11/08/2008 - how do I do this? p.s. I cannot change the field from a text to a date - it deletes all the info First, to change the text field to a date format: Format(DateSerial(Left([DateField],4),Mid([DateField],5,2),Right([DateField],2),"mm/dd/yyyy") It would probably be best to change the field to date type for programming and reporting down the line. To do this (first backup your data) then add a new field to your table (with a type of date). Do an update query to populate the new field with an ...

update many fields in a form from existing info.
I have 2 tables. One table stores various information about companies including company name, address, etc. The second table stores student names. In a form, I am able to enter mutiple student names into a memo field and, as of right now, type in the company name, address, etc. (it is not linked to the company name table at all). What I want to do is this: from the student add form, search the company table for a company based on partial name and/or zipcode, select one company from a possible 1 or more companies, and have the fields in the form automatically fill in - pulling the ...