Search in sources :

Example 1 with Constraint

use of cern.modesti.schema.category.Constraint in project modesti by jlsalmon.

the class CoreValidationService method validatePoints.

private boolean validatePoints(Request request, List<Category> categories) {
    boolean valid = true;
    for (Point point : request.getNonEmptyPoints()) {
        for (Category category : categories) {
            for (Field field : category.getFields()) {
                Object value = point.getValueByPropertyName(field.getPropertyName());
                // Check for invalid fields
                if (!isValidValue(value, point, field)) {
                    point.setValid(false);
                    valid = false;
                    point.addErrorMessage(category.getId(), field.getId(), "Value '" + value + "' is not a legal option for field '" + field.getName() + "'. Please select a value from the list.");
                }
                // Validate unique fields
                if (field.getUnique() != null) {
                    Constraint constraint = new Constraint("unique", Collections.singletonList(field.getId()), null);
                    if (!Constraints.validate(constraint, request, category)) {
                        valid = false;
                    }
                }
                // Required fields (can be simple boolean or condition list)
                boolean required = false;
                if (field.getRequired() instanceof Boolean && (Boolean) field.getRequired()) {
                    required = true;
                } else if (field.getRequired() != null) {
                    required = Conditionals.evaluate(field.getRequired(), point, request);
                }
                if (required) {
                    if (value == null || value.equals("")) {
                        point.setValid(false);
                        valid = false;
                        point.addErrorMessage(category.getId(), field.getId(), "'" + field.getName() + "' is mandatory");
                    }
                }
                // Min length
                if (field.getMinLength() != null) {
                    if (value != null && value.toString().length() < field.getMinLength()) {
                        point.setValid(false);
                        valid = false;
                        point.addErrorMessage(category.getId(), field.getId(), "'" + field.getName() + "' must be at least " + field.getMinLength() + " characters in length");
                    }
                }
                // Max length
                if (field.getMaxLength() != null) {
                    if (value != null && value.toString().length() > field.getMaxLength()) {
                        point.setValid(false);
                        valid = false;
                        point.addErrorMessage(category.getId(), field.getId(), "'" + field.getName() + "' must not exceed " + field.getMaxLength() + " characters in length");
                    }
                }
                // Numeric fields
                if (field.getType().equals("numeric")) {
                    if (value != null && !value.toString().isEmpty() && !NumberUtils.isNumber(value.toString())) {
                        point.setValid(false);
                        valid = false;
                        point.addErrorMessage(category.getId(), field.getId(), "Value for '" + field.getName() + "' must be numeric");
                    }
                }
            }
        }
    }
    return valid;
}
Also used : AutocompleteField(cern.modesti.schema.field.AutocompleteField) Field(cern.modesti.schema.field.Field) OptionsField(cern.modesti.schema.field.OptionsField) Category(cern.modesti.schema.category.Category) Constraint(cern.modesti.schema.category.Constraint) Point(cern.modesti.point.Point)

Aggregations

Point (cern.modesti.point.Point)1 Category (cern.modesti.schema.category.Category)1 Constraint (cern.modesti.schema.category.Constraint)1 AutocompleteField (cern.modesti.schema.field.AutocompleteField)1 Field (cern.modesti.schema.field.Field)1 OptionsField (cern.modesti.schema.field.OptionsField)1