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);
}
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);
}
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);
}
}
}
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);
}
}
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);
}
}
Aggregations