Overriding field length validation in the admin
The default length of a Hibernate string column is 255 characters. There are scenarios that this column length is too small. If you change the column length in the database you will find that the admin still validates the field for 255 characters or less. Within the Broadleaf Admin the default validation length is 255 characters as well. This default validation length is static and, in addition to changing the database column length, this validator value will need to be changed as well. in order to change the value, you will need to override the FieldLengthValidator class. The FieldLengthValidator class is used for all validation of field lengths. Here is a code example overriding this class and setting the max length to 2000 specifically for the SystemProperty Value field:
public class CustomFieldLengthValidator extends FieldLengthValidator { private static final Integer SYSTEM_PROPERTY_VALUE_LENGTH = 2000; @Override public PropertyValidationResult validate(Entity entity, Serializable instance, Map<String, FieldMetadata> entityFieldMetadata,BasicFieldMetadata propertyMetadata, String propertyName, String value) { // just changing the SystemProperty value field length if (instance instanceof SystemProperty) { String errorMessage = ""; boolean valid = StringUtils.length(value) <= SYSTEM_PROPERTY_VALUE_LENGTH; if (!valid) { BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();MessageSource messages = context.getMessageSource(); errorMessage = messages.getMessage("fieldLengthValidationFailure", new Object[]{SYSTEM_PROPERTY_VALUE_LENGTH, StringUtils.length(value)}, context.getJavaLocale());} return new PropertyValidationResult(valid, errorMessage); } } return super.validate(entity, instance, entityFieldMetadata, propertyMetadata, propertyName, value); }
Once the class is in place, you will need to register the new validator as a Spring bean names blFieldLengthValidator (e.g. in Java config AdminConfig.java)
public class AdminConfig { @Bean public FieldLengthValidator blFieldLengthValidator(){ return new CustomFieldLengthValidator(); } }
This example uses a static value for the new length. Other approaches can be explored to dynamically derive the length of a particular field/column.