Using a Validation Context


Both the Validate and Exchange methods have an optional third parameter, which is of type CValidateContext . By passing an instance of a CValidateContext , you can keep track of which input elements couldn t be validated or couldn t be exchanged. Using a validation context makes it very easy to keep track of which input elements were incorrectly entered. One common use for the validation context is to highlight incorrectly entered information for the user so that he or she can easily identify these items and re-enter them.

The CValidateContext class takes ones optional DWORD parameter in its constructor. The DWORD input can be any combination of the following:

  • VALIDATION_S_OK

  • VALIDATION_S_EMPTY

  • VALIDATION_E_PARAMNOTFOUND

  • VALIDATION_E_INVALIDPARAM

  • VALIDATION_E_FAIL

This parameter is used to specify what types of validation errors will constitute an actual error for a given call to exchange. The use of this parameter should become clearer as you continue building on your example application and use the combination of VALIDATION_E_PARAMNOTFOUNDVALIDATION_S_EMPTY to specify that you want missing or empty parameters to constitute an error. To do this, you ll need to keep track of these parameters:

 // use a validation context object to keep track of our errors  CValidateContext validateContext(VALIDATION_E_PARAMNOTFOUND   VALIDATION_S_EMPTY);  // get our value of 'fruit' as a CString  CString fruit;  requestParams->Exchange("fruit", &fruit, &validateContext);  // get the value of 'quantity' as an integer  int quantity;  requestParams->Exchange("quantity", &quantity, &validateContext);  // get the value of 'isorganic' as a boolean  bool isOrganic;  requestParams->Exchange("is_organic", &isOrganic, &validateContext); 

The CValidateContext::ParamsOK method will return true if any of the parameters fell under the error criteria specified in your DWORD input. There are also a number of methods in the CValidateContext class to retrieve the number of results that had errors, as well as specific errors for specific variables .

To illustrate these uses, you ll continue to expand on your use of CValidateContext in the example. To begin, you ll make the CValidateContext object from the previous example a class member, and you ll add some replacement handlers to display errors beside each of the form elements that is incorrectly filled out.

Listing 6-5 is a complete listing of the sample that you started working on at the beginning of this chapter. This example should serve as a good review of everything that you ve looked at in this chapter.

Listing 6.5: Using a Validation Context in ValidateAndExchange
start example
 1 [ request_handler("Default") ]  2 class CUserInputExample  3 {  4 private:  5    CValidateContext m_validateContext;  6    bool                      m_formHasBeenSubmitted;  7  8 public:  9    HTTP_CODE ValidateAndExchange()  10   {  11       m_HttpResponse.SetContentType("text/html");  12  13       const CHttpRequestParams *requestParams(NULL);  14       if (m_HttpRequest.GetMethod() ==               CHttpRequest::HTTP_METHOD::HTTP_METHOD_POST)  15       {  16           requestParams = &(this->m_HttpRequest.GetFormVars());  17       }  18       else  19       {  20           requestParams = &(this->m_HttpRequest.GetQueryParams());  21       }  22  23       m_formHasBeenSubmitted = (requestParams->GetCount() == 0) ?  false : true;  24  25       if (!m_formHasBeenSubmitted)  26       {  27           return HTTP_SUCCESS;  28       }  29  30       m_validateContext.m_dwFlags = VALIDATION_S_EMPTY   VALIDATION_E_PARAMNOTFOUND;  31  32       CString fruit;  33       requestParams->Exchange("fruit", &fruit, &m_validateContext);  34  35       bool isOrganic;  36       requestParams->Exchange("is_organic", &isOrganic, &m_validateContext);  37  38       int quantity;  39       requestParams->Exchange("quantity", &quantity, &m_validateContext);  40  41       return HTTP_SUCCESS;  42  }  43  44 protected:  45   [tag_name("HasErrors")]  46   HTTP_CODE OnHasErrors()  47   {  48       if (!m_formHasBeenSubmitted)  49       {  50           return HTTP_S_FALSE;  51       }  52       else  53       {  54           return (!m_validateContext.ParamsOK()) ? HTTP_SUCCESS : HTTP_S_FALSE;  55       }  56   }  57  58   [tag_name("GetError")]  59   HTTP_CODE OnGetError(TCHAR *szName)  60   {  61       if (!m_formHasBeenSubmitted)  62       {  63           return HTTP_SUCCESS;  64       }  65  66       DWORD errorType;  67       if (!m_validateContext.GetResultAt(szName, errorType))  68       {  69           return HTTP_SUCCESS;  70       }  71       else  72       {  73           m_HttpResponse << "A value for " << szName;  74  75           switch (errorType)  76           {  77               case VALIDATION_S_EMPTY:  78                   m_HttpResponse << " cannot be an empty value!";  79                   break;  80  81               case VALIDATION_E_PARAMNOTFOUND:  82                   m_HttpResponse << " was not found!";  83                   break;  84  85               case VALIDATION_E_INVALIDPARAM:  86                   m_HttpResponse << " was entered, but in an incorrect format!";  87                   break;  88  89               default:  90                   m_HttpResponse << " was not a valid value!";  91                   break;  92           }  93       }  94  95       return HTTP_SUCCESS;  96   }  97 }; 
end example
 

Let s take a look at this example line-by-line :

  • Line 5: This validation context object will be used by your replacement handlers to display errors.

  • Line 6: This flag will determine whether or not the user has submitted the form. If the user hasn t submitted the form, then you don t want to do any validation.

  • Line 23: Once you know how the form is being submitted, look at the number of request parameters being submitted. Even if the user didn t enter any values, the request parameters are submitted with empty values, so the number isn t 0. If the user hasn t submitted the form, you want to set your flag and return HTTP_SUCCESS . The flag will tell your tag replacement handlers not to do anything because the form hasn t been submitted. Returning HTTP_SUCCESS will allow the rest of your SRF file to be processed .

  • Line 30: The m_dwFlags member in the CValidateContext class determines what types of validation results you want to specify as being invalid. In this example, you want to specify both empty and missing values as being errors. You don t necessarily need to specify both types, but we ve done so for the purposes of this example.

  • Lines 32 through 39: These lines are taken from the previous section. They get the value of fruit as a CString .

The SRF file corresponding to this application is shown in Listing 6-6.

Listing 6.6: Source for SRF That Requires Validation
start example
 {{handler userinput.dll/Default}}  <html>    <body>      <form method="post">      <table border=1>      <tr>      <td colspan=2>        Welcome to Ben's World of Fruit.      </td>      </tr>      <tr>      <td>Please select an item:</td>      <td>        <ul>        <li><input type="radio" name="fruit" value="apple" ID="Radio1">Apple        <li><input type="radio" name="fruit" value="peach" ID="Radio2">Peach        <li><input type="radio" name="fruit" value="orange" ID="Radio3">Orange        </ul>        {{if HasErrors}}          <br><font color="red">{{GetError(fruit)}}        {{endif}}      </td>      </tr>      <tr>      <td>Type:</td>      <td>        <input type="radio" value=1 name="is_organic">Organic        <input type="radio" value=0 name="is_organic">Conventional        {{if HasErrors}}          <br><font color="red">{{GetError(is_organic)}}</font>        {{endif}}      </td>      </tr>      <tr>      <td>Quantity:</td>      <td>        <input name="quantity">        {{if HasErrors}}          <br><font color="red">{{GetError(quantity)}}</font>        {{endif}}      </td>      </tr>      <tr>      <td colspan=2>        <input type="submit" value="Go">        {{if HasErrors}}          <br><font color="red">Please correct your errors!</font>        {{endif}}      </td>      </tr>      </table>      </form>    </body>  </html> 
end example
 

As you can see from this example, using a validation context helps you quickly put together a user-friendly interface for your Web application.




ATL Server. High Performance C++ on. NET
Observing the User Experience: A Practitioners Guide to User Research
ISBN: B006Z372QQ
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net