Search in sources :

Example 16 with BindingResult

use of org.springframework.validation.BindingResult in project alf.io by alfio-event.

the class ReservationFlowIntegrationTest method reserveTicket.

private String reserveTicket(String eventName) {
    ReservationForm reservationForm = new ReservationForm();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    ServletWebRequest servletWebRequest = new ServletWebRequest(request);
    BindingResult bindingResult = new BeanPropertyBindingResult(reservationForm, "reservation");
    Model model = new BindingAwareModelMap();
    RedirectAttributes redirectAttributes = new RedirectAttributesModelMap();
    TicketReservationModification ticketReservation = new TicketReservationModification();
    ticketReservation.setAmount(1);
    ticketReservation.setTicketCategoryId(ticketCategoryRepository.findByEventId(event.getId()).stream().findFirst().map(TicketCategory::getId).orElseThrow(IllegalStateException::new));
    reservationForm.setReservation(Collections.singletonList(ticketReservation));
    return eventController.reserveTicket(eventName, reservationForm, bindingResult, model, servletWebRequest, redirectAttributes, Locale.ENGLISH);
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Model(org.springframework.ui.Model) BindingAwareModelMap(org.springframework.validation.support.BindingAwareModelMap) ReservationForm(alfio.controller.form.ReservationForm) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) TicketReservationModification(alfio.model.modification.TicketReservationModification)

Example 17 with BindingResult

use of org.springframework.validation.BindingResult in project alf.io by alfio-event.

the class ReservationFlowIntegrationTest method payOffline.

private String payOffline(String eventName, String reservationIdentifier) {
    PaymentForm paymentForm = new PaymentForm();
    paymentForm.setPaymentMethod(PaymentProxy.OFFLINE);
    paymentForm.setEmail("test@test.com");
    paymentForm.setBillingAddress("my billing address");
    paymentForm.setFirstName("full");
    paymentForm.setLastName("name");
    paymentForm.setTermAndConditionsAccepted(true);
    paymentForm.setPostponeAssignment(true);
    BindingResult bindingResult = new BeanPropertyBindingResult(paymentForm, "paymentForm");
    Model model = new BindingAwareModelMap();
    MockHttpServletRequest request = new MockHttpServletRequest();
    RedirectAttributes redirectAttributes = new RedirectAttributesModelMap();
    return reservationController.handleReservation(eventName, reservationIdentifier, paymentForm, bindingResult, model, request, Locale.ENGLISH, redirectAttributes);
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) PaymentForm(alfio.controller.form.PaymentForm) Model(org.springframework.ui.Model) BindingAwareModelMap(org.springframework.validation.support.BindingAwareModelMap) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap)

Example 18 with BindingResult

use of org.springframework.validation.BindingResult in project alf.io by alfio-event.

the class PaymentForm method validate.

public void validate(BindingResult bindingResult, TotalPrice reservationCost, Event event, List<TicketFieldConfiguration> fieldConf) {
    List<PaymentProxy> allowedPaymentMethods = event.getAllowedPaymentProxies();
    Optional<PaymentProxy> paymentProxyOptional = Optional.ofNullable(paymentMethod);
    PaymentProxy paymentProxy = paymentProxyOptional.filter(allowedPaymentMethods::contains).orElse(PaymentProxy.STRIPE);
    boolean priceGreaterThanZero = reservationCost.getPriceWithVAT() > 0;
    boolean multiplePaymentMethods = allowedPaymentMethods.size() > 1;
    if (multiplePaymentMethods && priceGreaterThanZero && !paymentProxyOptional.isPresent()) {
        bindingResult.reject(ErrorsCode.STEP_2_MISSING_PAYMENT_METHOD);
    } else if (priceGreaterThanZero && (paymentProxy == PaymentProxy.STRIPE && StringUtils.isBlank(stripeToken))) {
        bindingResult.reject(ErrorsCode.STEP_2_MISSING_STRIPE_TOKEN);
    }
    if (Objects.isNull(termAndConditionsAccepted) || !termAndConditionsAccepted) {
        bindingResult.reject(ErrorsCode.STEP_2_TERMS_NOT_ACCEPTED);
    }
    email = StringUtils.trim(email);
    fullName = StringUtils.trim(fullName);
    firstName = StringUtils.trim(firstName);
    lastName = StringUtils.trim(lastName);
    billingAddress = StringUtils.trim(billingAddress);
    ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult, "email", ErrorsCode.STEP_2_EMPTY_EMAIL);
    rejectIfOverLength(bindingResult, "email", ErrorsCode.STEP_2_MAX_LENGTH_EMAIL, email, 255);
    if (event.mustUseFirstAndLastName()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult, "firstName", ErrorsCode.STEP_2_EMPTY_FIRSTNAME);
        rejectIfOverLength(bindingResult, "firstName", ErrorsCode.STEP_2_MAX_LENGTH_FIRSTNAME, fullName, 255);
        ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult, "lastName", ErrorsCode.STEP_2_EMPTY_LASTNAME);
        rejectIfOverLength(bindingResult, "lastName", ErrorsCode.STEP_2_MAX_LENGTH_LASTNAME, fullName, 255);
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult, "fullName", ErrorsCode.STEP_2_EMPTY_FULLNAME);
        rejectIfOverLength(bindingResult, "fullName", ErrorsCode.STEP_2_MAX_LENGTH_FULLNAME, fullName, 255);
    }
    rejectIfOverLength(bindingResult, "billingAddress", ErrorsCode.STEP_2_MAX_LENGTH_BILLING_ADDRESS, billingAddress, 450);
    if (email != null && !email.contains("@") && !bindingResult.hasFieldErrors("email")) {
        bindingResult.rejectValue("email", ErrorsCode.STEP_2_INVALID_EMAIL);
    }
    if (hasPaypalTokens() && !PaypalManager.isValidHMAC(new CustomerName(fullName, firstName, lastName, event), email, billingAddress, hmac, event)) {
        bindingResult.reject(ErrorsCode.STEP_2_INVALID_HMAC);
    }
    if (!postponeAssignment) {
        boolean success = Optional.ofNullable(tickets).filter(m -> !m.isEmpty()).map(m -> m.entrySet().stream().map(e -> Validator.validateTicketAssignment(e.getValue(), fieldConf, Optional.empty(), event))).filter(s -> s.allMatch(ValidationResult::isSuccess)).isPresent();
        if (!success) {
            bindingResult.reject(ErrorsCode.STEP_2_MISSING_ATTENDEE_DATA);
        }
    }
}
Also used : PaymentProxy(alfio.model.transaction.PaymentProxy) ValidationResult(alfio.model.result.ValidationResult) java.util(java.util) PaypalManager(alfio.manager.PaypalManager) PaymentProxy(alfio.model.transaction.PaymentProxy) ValidationUtils(org.springframework.validation.ValidationUtils) alfio.model(alfio.model) Data(lombok.Data) BindingResult(org.springframework.validation.BindingResult) ErrorsCode(alfio.util.ErrorsCode) Validator(alfio.util.Validator) StringUtils(org.apache.commons.lang3.StringUtils) Serializable(java.io.Serializable) ValidationResult(alfio.model.result.ValidationResult)

Example 19 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class PageService method restorePageConfiguration.

@Override
public PageConfigurationDto restorePageConfiguration(String pageCode) {
    try {
        IPage pageD = this.loadPage(pageCode, STATUS_DRAFT);
        if (null == pageD) {
            throw new ResourceNotFoundException(ERRCODE_PAGE_NOT_FOUND, "page", pageCode);
        }
        IPage pageO = this.loadPage(pageCode, STATUS_ONLINE);
        if (null == pageO) {
            DataBinder binder = new DataBinder(pageCode);
            BindingResult bindingResult = binder.getBindingResult();
            bindingResult.reject(ERRCODE_STATUS_INVALID, new String[] { pageCode }, "page.status.invalid");
            throw new ValidationGenericException(bindingResult);
        }
        pageD.setMetadata(pageO.getMetadata());
        pageD.setWidgets(pageO.getWidgets());
        this.getPageManager().updatePage(pageD);
        PageConfigurationDto pageConfigurationDto = new PageConfigurationDto(pageO, STATUS_ONLINE);
        return pageConfigurationDto;
    } catch (ApsSystemException e) {
        logger.error("Error restoring page {} configuration", pageCode, e);
        throw new RestServerError("error in restoring page configuration", e);
    }
}
Also used : IPage(com.agiletec.aps.system.services.page.IPage) BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RestServerError(org.entando.entando.aps.system.exception.RestServerError) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) DataBinder(org.springframework.validation.DataBinder) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) PageConfigurationDto(org.entando.entando.aps.system.services.page.model.PageConfigurationDto)

Example 20 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class ApiConsumerServiceImpl method validateForCreate.

private void validateForCreate(ApiConsumer consumer) throws ApsSystemException {
    if (consumerManager.getConsumerRecord(consumer.getKey()) != null) {
        DataBinder binder = new DataBinder(consumer);
        BindingResult bindingResult = binder.getBindingResult();
        bindingResult.reject(ERRCODE_CONSUMER_ALREADY_EXISTS, new String[] { consumer.getKey() }, "api.consumer.exists");
        throw new ValidationConflictException(bindingResult);
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) DataBinder(org.springframework.validation.DataBinder) ValidationConflictException(org.entando.entando.web.common.exceptions.ValidationConflictException)

Aggregations

BindingResult (org.springframework.validation.BindingResult)140 Test (org.junit.Test)28 Test (org.junit.jupiter.api.Test)27 List (java.util.List)23 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)23 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 ResponseEntity (org.springframework.http.ResponseEntity)20 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)20 Collectors (java.util.stream.Collectors)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 FieldError (org.springframework.validation.FieldError)16 ObjectError (org.springframework.validation.ObjectError)16 Model (org.springframework.ui.Model)15 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)14 HashMap (java.util.HashMap)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 DefaultProblem (org.zalando.problem.DefaultProblem)13 Problem (org.zalando.problem.Problem)13