cancel
Showing results for 
Search instead for 
Did you mean: 

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

RaufAliev
Participant
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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)

Answers (0)