@Override public Page populate(Page page, HttpServletRequest req) throws ValidationException { ArrayList<ValidationError> validationErrors = new ArrayList<ValidationError>(); String alias = ValidationUtils.validateNotEmptyParameter("alias", req, validationErrors); String name = ValidationUtils.validateNotEmptyParameter("name", req, validationErrors); String description = ValidationUtils.validateNotEmptyParameter("description", req, validationErrors); String text = ValidationUtils.validateNotEmptyParameter("text", req, validationErrors); if (!validationErrors.isEmpty()) { throw new ValidationException(validationErrors); } else { page.setAlias(alias); page.setName(name); page.setDescription(description); page.setText(text); page.setAdminAccess(req.getParameter("adminAccess") != null); page.setAnonymousAccess(req.getParameter("anonymousAccess") != null); page.setUserAccess(req.getParameter("userAccess") != null); page.setVisibleInMenu(req.getParameter("visibleInMenu") != null); page.setEnabled(req.getParameter("enabled") != null); page.setBreadCrumb(req.getParameter("breadCrumb") != null); String[] allowedUserIDs = req.getParameterValues("user"); ArrayList<Integer> userIDs = null; if (allowedUserIDs != null) { userIDs = NumberUtils.toInt(allowedUserIDs); } page.setAllowedUserIDs(userIDs); String[] allowedGroupIDs = req.getParameterValues("group"); ArrayList<Integer> groupIDs = null; if (allowedGroupIDs != null) { groupIDs = NumberUtils.toInt(allowedGroupIDs); } page.setAllowedGroupIDs(groupIDs); return page; } }
public static <T> List<T> validateParameters( String fieldName, HttpServletRequest req, boolean required, Integer minLength, Integer maxLength, BeanStringPopulator<T> typePopulator, Collection<ValidationError> errors) { String[] parameterValues = req.getParameterValues(fieldName); if (parameterValues == null || parameterValues.length == 0 || !ValidationUtils.checkParameterValueLengths(parameterValues, 1)) { if (required) { errors.add(new ValidationError(fieldName, ValidationErrorType.RequiredField)); } return null; } List<T> values = new ArrayList<T>(); for (String value : parameterValues) { value = value.trim(); if (maxLength != null && value.length() > maxLength) { errors.add(new ValidationError(fieldName, ValidationErrorType.TooLong)); return null; } else if (minLength != null && value.length() < minLength) { errors.add(new ValidationError(fieldName, ValidationErrorType.TooShort)); return null; } if (!typePopulator.validateFormat(value)) { errors.add(new ValidationError(fieldName, ValidationErrorType.InvalidFormat)); return null; } values.add(typePopulator.getValue(value)); } return values; }