Search in sources :

Example 66 with BasicFieldMetadata

use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.

the class RequiredIfPropertyValidator method validate.

@Override
public PropertyValidationResult validate(Entity entity, Serializable instance, Map<String, FieldMetadata> entityFieldMetadata, Map<String, String> validationConfiguration, BasicFieldMetadata propertyMetadata, String propertyName, String value) {
    String errorMessage = "";
    String compareFieldName = lookupCompareFieldName(propertyName, validationConfiguration);
    String compareFieldValue = validationConfiguration.get("compareFieldValue");
    String compareFieldRegEx = validationConfiguration.get("compareFieldRegEx");
    Property compareFieldProperty = null;
    boolean valid = true;
    if (StringUtils.isEmpty(value)) {
        compareFieldProperty = entity.getPMap().get(compareFieldName);
        if (compareFieldProperty != null) {
            if (compareFieldValue != null) {
                valid = !compareFieldValue.equals(compareFieldProperty.getValue());
            } else if (compareFieldRegEx != null) {
                String expression = validationConfiguration.get("compareFieldRegEx");
                valid = !compareFieldProperty.getValue().matches(expression);
            }
        }
    }
    if (!valid) {
        BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
        MessageSource messages = context.getMessageSource();
        FieldMetadata fmd = entityFieldMetadata.get(compareFieldName);
        String fieldName = messages.getMessage(fmd.getFriendlyName(), null, context.getJavaLocale());
        errorMessage = messages.getMessage("requiredIfValidationFailure", new Object[] { fieldName, compareFieldProperty.getValue() }, context.getJavaLocale());
    }
    return new PropertyValidationResult(valid, errorMessage);
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) MessageSource(org.springframework.context.MessageSource) Property(org.broadleafcommerce.openadmin.dto.Property)

Aggregations

BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)66 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)33 Property (org.broadleafcommerce.openadmin.dto.Property)28 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 Map (java.util.Map)14 Entity (org.broadleafcommerce.openadmin.dto.Entity)11 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)10 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)9 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)9 Field (java.lang.reflect.Field)8 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)8 ServiceException (org.broadleafcommerce.common.exception.ServiceException)7 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)7 ComboField (org.broadleafcommerce.openadmin.web.form.entity.ComboField)7 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)7 Serializable (java.io.Serializable)6 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)6 MetadataOverride (org.broadleafcommerce.openadmin.dto.override.MetadataOverride)6 FieldInfo (org.broadleafcommerce.openadmin.server.dao.FieldInfo)6