I have plenty of examples from our application.

The first is a case where the user must enter at least one phone
number and if the type of phone is selected, then the user must enter
a phone number.  The validation code is as follows:

    if(!hasFieldErrors("exampleData.phoneNumber1") &&
       !hasFieldErrors("exampleData.phoneNumber2") &&
       !hasFieldErrors("exampleData.phoneNumber3") &&
       !hasFieldErrors("exampleData.phoneNumber4"))
    {
      if((!exampleData.getPhoneNumber1().equals("")&&
exampleData.getPhoneType1()== 0) ||
         (!exampleData.getPhoneNumber2().equals("")&&
exampleData.getPhoneType2()== 0) ||
         (!exampleData.getPhoneNumber3().equals("")&&
exampleData.getPhoneType3()== 0) ||
         (!exampleData.getPhoneNumber4().equals("")&&
exampleData.getPhoneType4()== 0))
      {
        addActionError(getText("phoneType.error"));
      }

      if((exampleData.getPhoneNumber1().equals("")&&
exampleData.getPhoneType1()!= 0) ||
         (exampleData.getPhoneNumber2().equals("")&&
exampleData.getPhoneType2()!= 0) ||
         (exampleData.getPhoneNumber3().equals("")&&
exampleData.getPhoneType3()!= 0) ||
         (exampleData.getPhoneNumber4().equals("")&&
exampleData.getPhoneType4()!= 0))
      {
        addActionError(getText("phoneNumber.error"));
      }
    }

Note, I did not write this, this is taken straight from our source code.

The second example I have is a case where we want to use some logic to
validate prescription information.  The validation for prescriptions
is tricky because the user doesn't have to enter prescriptions, but if
anything is entered for a prescription, then the prescription data is
fully validated.  Note that we have several places in the app where
this is done so I'd prefer not to duplicate this validation logic in
all the places we need it.  (i.e. don't want to violate the DRY
principle)  I broke this out into a helper class for reusability.  The
isEmpty() method on PrescriptionData checks to see if there is any
data populated.

  public void validate(String propertyPrefix, List prescriptionList,
ValidationAware errors, TextProvider textProvider)
  {
    for (int i = 0; i < prescriptionList.size(); i++)
    {
      PrescriptionData data = (PrescriptionData) prescriptionList.get(i);
      // pull out conversion errors and set original values
      ActionInvocation invocation =
ActionContext.getContext().getActionInvocation();
      Map conversionErrors =
invocation.getInvocationContext().getConversionErrors();
      if (!data.isEmpty())
      {
        if (data.getApplicantId() == 0)
        {
          errors.addFieldError("medication", textProvider
            .getText("medicalQuestions.applicant.required"));
        }
        if (ValidationUtils.isStringEmpty(data.getMedication()))
        {
          errors.addFieldError("medication", textProvider
            .getText("medicalQuestions.medication.required"));
        }
        if (ValidationUtils.isStringEmpty(data.getDescription()))
        {
          errors.addFieldError("description", textProvider
            .getText("medicalQuestions.dosage.required"));
        }

        if (!hasConversionError(propertyPrefix + "[" + i +
"].startDate", conversionErrors) && data.getStartDate() == null)
        {
          errors.addFieldError("startDate", textProvider
            .getText("medicalQuestions.startDate.required"));
        }
        if (data.getStartDate() != null && data.getEndDate() != null)
        {
          Calendar startCal = Calendar.getInstance();
          startCal.setTime(data.getStartDate());
          Calendar endCal = Calendar.getInstance();
          endCal.setTime(data.getEndDate());
          if (startCal.after(endCal))
          {
            
errors.addActionError(textProvider.getText("medicalQuestions.startAfterEnd"));
          }
        }
      }
    }
  }

The final example is a case where some might consider it a 'business
rule', however, in my opinion, validation is validation regardless of
whether you're implementing business rule validation or simple UI
validation.  My argument for this is the fact that the simple UI
validation is used as part of the business rule validation.  (For
example, you might need to check that a string is not blank or null)
In this use case, we allow the user to enter a state and a zip and we
verify that the state and zip match.  We do this by making sure that
the combination of state and zip produce at least one valid county.

    if (!hasFieldErrors("siteLocationView.zip")
      && !hasFieldErrors("siteLocationView.state"))
    {
      CountyData counties[] =
siteLocationProcess.getCountiesForZipAndState(siteLocationView
        .getZip(), siteLocationView.getStateAbbr());
      if (counties.length < 1)
      {
        addActionError(getText("siteLocationView.zipcountymismatch.error"));
      }
    }

This is a case where the validation is dependent on a business process object.

There's plenty more where that came from, so if you need more
examples, I'd be happy to provide them.  These examples show how
verbose the more involved validation can be as well as the inability
to easily reuse validation out-of-the-box.
Tom



On Dec 12, 2007 7:54 AM, Ted Husted <[EMAIL PROTECTED]> wrote:
> A good place to start might be with a set of use cases that
> demonstrate various validation scenarios. We could then try to
> implement the use cases using the XW validation, Commons validator,
> and Hibernate Validator, and compare the outcome.
>
> -- HTH, Ted
>  * <http://www.StrutsMentor.com/>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to