Skip to Content

Country <-> Regions: Why are the validation rules HARDCODED? + OCC v2 question.

Are there any chances to figure out what countries require region code and what not through API OCC v2? It seems that it is not possible. And it turns out that I need to modify the client application each time I add regions to the without-region countries on the backend side.

It seems that it is a bad design.

I found that de.hybris.platform.acceleratorstorefrontcommons.forms.validation.AddressValidator has hardcoded checks. It seems that you have all the data to perform these checks without hardcoding.

 protected void validateCountrySpecificFields(final AddressForm addressForm, final Errors errors)
 {
    final String isoCode = addressForm.getCountryIso();
    if (isoCode != null)
    {
       switch (CountryCode.lookup(isoCode))
       {
          case CHINA:
             validateStringField(addressForm.getTitleCode(), AddressField.TITLE, MAX_FIELD_LENGTH, errors);
             validateFieldNotNull(addressForm.getRegionIso(), AddressField.REGION, errors);
             break;
          case CANADA:
             validateStringField(addressForm.getTitleCode(), AddressField.TITLE, MAX_FIELD_LENGTH, errors);
             validateFieldNotNull(addressForm.getRegionIso(), AddressField.REGION, errors);
             break;
          case USA:
             validateStringField(addressForm.getTitleCode(), AddressField.TITLE, MAX_FIELD_LENGTH, errors);
             validateFieldNotNull(addressForm.getRegionIso(), AddressField.REGION, errors);
             break;
          case JAPAN:
             validateFieldNotNull(addressForm.getRegionIso(), AddressField.REGION, errors);
             validateStringField(addressForm.getLine2(), AddressField.LINE2, MAX_FIELD_LENGTH, errors);
             break;
          default:
             validateStringField(addressForm.getTitleCode(), AddressField.TITLE, MAX_FIELD_LENGTH, errors);
             break;
       }
    }
 }

It seems that it is a bad design too.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Aug 11, 2016 at 09:41 AM

    The AddressValidator you've provided is from Accelerator storefront. OCC uses different validators.

    See the following file: ycommercewebservices/web/webroot/WEB-INF/config/v2/validators-v2-spring.xml

    The responsible bean is addressValidator/defaultAddressValidator, which is an instance of CountryAwareAddressValidator which declares specific validators for given countries (like China, Japan) and the fallback one for all other countries.

    Here is the documentation:
    https://help.hybris.com/6.1.0/hcd/8b4d393886691014bcf881fe54511b8e.html
    The page is for v1 version of OCC, but the validators work in the same way in v2.

    Note that there is a OCC call:
    POST /users/{userId}/addresses/verification
    It allows to verify address without saving it. It may be helpful in this scenario. (See the docs: https://help.hybris.com/6.1.0/api/ycommercewebservices/main.html)

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.