3 - Validating the T-shirt form bean
3 - Validating the T-shirt form bean
After creating the T-Shirt form bean, we'll add some validation on the T-shirt form data. In this tutorial, we will add a restriction on the T-shirt text's length while allowing any type of character:
- In com.extensions.tshirt.sfweb.formbean, create a new package named validator.impl
- In com.extensions.tshirt.sfweb.formbean.validator.impl, create a Java class named TshirtFormBeanValidator and add the following code below:
CoreTutorial2/tutorial2-ext-storefront/src/main/java/com/extensions/tshirt/sfweb/formbean/validator/impl/TshirtFormBeanValidator.java package com.extensions.tshirt.sfweb.formbean.validator.impl; import java.util.Map; import com.extensions.tshirt.sfweb.formbean.ExtShoppingItemFormBean; import com.extensions.tshirt.sfweb.formbean.TshirtFormBean; import org.owasp.esapi.ESAPI; import org.springframework.validation.Errors; import com.elasticpath.sfweb.formbean.ShoppingItemFormBean; import com.elasticpath.sfweb.formbean.validator.impl.ProductFormBeanValidator; /** * Validates {@link TshirtFormBean} data. */ public class TshirtFormBeanValidator extends ProductFormBeanValidator { private static final String ESAPI_ANYTHING_VALIDATOR = "Anything"; private static final int FRONT_MAX_LEN = 100; private static final int BACK_MAX_LEN = 100; private Map<String, String> errorCodeMap; /** * {@inheritDoc} * * Ensures the T-Shirt's front and back text are not greater than 100 characters. */ public void validate(final ShoppingItemFormBean formBean, final Errors errors, final int shoppingItemIndex) { // validate quantity super.validate(formBean, errors, shoppingItemIndex); // validate t-shirt front and back text lengths ExtShoppingItemFormBean extShoppingItemFormBean = (ExtShoppingItemFormBean) formBean; TshirtFormBean tshirtFormBean = extShoppingItemFormBean.getTshirtFields(); validateFrontText(tshirtFormBean, errors, shoppingItemIndex); validateBackText(tshirtFormBean, errors, shoppingItemIndex); } private void validateFrontText(final TshirtFormBean tshirtFormBean, final Errors errors, final int shoppingItemIndex) { String prefix = "cartItems[" + shoppingItemIndex + "].tshirtFields."; boolean frontTextisValid = ESAPI.validator().isValidInput("tshirt_frontText", tshirtFormBean.getFrontText(), ESAPI_ANYTHING_VALIDATOR, FRONT_MAX_LEN, true); if (!frontTextisValid) { addError(prefix + "frontText", getErrorCodeMap().get("errorFrontTextSize"), errors); } } private void validateBackText(final TshirtFormBean tshirtFormBean, final Errors errors, final int shoppingItemIndex) { String prefix = "cartItems[" + shoppingItemIndex + "].tshirtFields."; boolean backTextisValid = ESAPI.validator().isValidInput("tshirt_backText", tshirtFormBean.getBackText(), ESAPI_ANYTHING_VALIDATOR, BACK_MAX_LEN, true); if (!backTextisValid) { addError(prefix + "backText", getErrorCodeMap().get("errorBackTextSize"), errors); } } public Map<String, String> getErrorCodeMap() { return errorCodeMap; } public void setErrorCodeMap(final Map<String, String> errorCodeMap) { this.errorCodeMap = errorCodeMap; } }
- In ext-storefront-webapp/src/main/webapp/WEB-INF/security/ESAPI.properties, add the following property at the end of the file:
Validator.Anything=^.*$
The TshirtFormBeanValidator will use this regular expression to allow any character in the front and back text fields.