Search in sources :

Example 1 with TicketFieldConfiguration

use of alfio.model.TicketFieldConfiguration in project alf.io by alfio-event.

the class Validator method validateTicketAssignment.

public static ValidationResult validateTicketAssignment(UpdateTicketOwnerForm form, List<TicketFieldConfiguration> additionalFieldsForEvent, Optional<Errors> errorsOptional, Event event) {
    if (!errorsOptional.isPresent()) {
        // already validated
        return ValidationResult.success();
    }
    Errors errors = errorsOptional.get();
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "error.email");
    String email = form.getEmail();
    if (!isEmailValid(email)) {
        errors.rejectValue("email", "error.email");
    }
    if (event.mustUseFirstAndLastName()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", ErrorsCode.STEP_2_EMPTY_FIRSTNAME);
        validateMaxLength(form.getFirstName(), "firstName", ErrorsCode.STEP_2_MAX_LENGTH_FIRSTNAME, 255, errors);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", ErrorsCode.STEP_2_EMPTY_LASTNAME);
        validateMaxLength(form.getLastName(), "lastName", ErrorsCode.STEP_2_MAX_LENGTH_LASTNAME, 255, errors);
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "fullName", ErrorsCode.STEP_2_EMPTY_FULLNAME);
        validateMaxLength(form.getFullName(), "fullName", ErrorsCode.STEP_2_MAX_LENGTH_FULLNAME, 255, errors);
    }
    // 
    for (TicketFieldConfiguration fieldConf : additionalFieldsForEvent) {
        boolean isField = form.getAdditional() != null && form.getAdditional().containsKey(fieldConf.getName());
        if (!isField) {
            continue;
        }
        form.getAdditional().get(fieldConf.getName()).forEach(formValue -> {
            if (fieldConf.isMaxLengthDefined()) {
                validateMaxLength(formValue, "additional['" + fieldConf.getName() + "']", "error." + fieldConf.getName(), fieldConf.getMaxLength(), errors);
            }
            if (!fieldConf.getRestrictedValues().isEmpty()) {
                validateRestrictedValue(formValue, "additional['" + fieldConf.getName() + "']", "error." + fieldConf.getName(), fieldConf.getRestrictedValues(), errors);
            }
            if (fieldConf.isRequired() && StringUtils.isBlank(formValue)) {
                errors.rejectValue("additional['" + fieldConf.getName() + "']", "error." + fieldConf.getName());
            }
        });
    // TODO: complete checks: min length
    }
    return evaluateValidationResult(errors);
}
Also used : Errors(org.springframework.validation.Errors) TicketFieldConfiguration(alfio.model.TicketFieldConfiguration)

Aggregations

TicketFieldConfiguration (alfio.model.TicketFieldConfiguration)1 Errors (org.springframework.validation.Errors)1