@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);
  }
  @Override
  public Entity update(
      PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper)
      throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
      PersistencePerspective persistencePerspective =
          persistencePackage.getPersistencePerspective();
      Map<String, FieldMetadata> adminProperties =
          helper.getSimpleMergedProperties(AdminUser.class.getName(), persistencePerspective);
      Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
      AdminUser adminInstance =
          (AdminUser) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);

      Entity errorEntity = validateLegalUsernameAndEmail(entity, adminInstance, false);
      if (errorEntity != null) {
        return errorEntity;
      }

      String passwordBefore = adminInstance.getPassword();
      adminInstance.setPassword(null);
      adminInstance =
          (AdminUser) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
      Property passwordProperty = entity.getPMap().get("password");
      if (passwordProperty != null) {
        if (StringUtils.isNotEmpty(passwordProperty.getValue())) {
          adminInstance.setUnencodedPassword(passwordProperty.getValue());
          adminInstance.setPassword(null);
        } else {
          adminInstance.setPassword(passwordBefore);
        }
      }

      validateUserUpdateSecurity(persistencePackage, adminInstance);

      adminInstance = adminSecurityService.saveAdminUser(adminInstance);
      Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);

      return adminEntity;

    } catch (Exception e) {
      throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
  }