use of org.springframework.validation.MapBindingResult in project spring-boot by spring-projects.
the class DefaultErrorAttributesTests method withoutBindingErrors.
@Test
void withoutBindingErrors() {
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new BindException(bindingResult);
testBindingResult(bindingResult, ex, ErrorAttributeOptions.defaults());
}
use of org.springframework.validation.MapBindingResult in project spring-boot by spring-projects.
the class ValidatorAdapterTests method wrapLocalValidatorFactoryBean.
@Test
void wrapLocalValidatorFactoryBean() {
this.contextRunner.withUserConfiguration(LocalValidatorFactoryBeanConfig.class).run((context) -> {
ValidatorAdapter wrapper = context.getBean(ValidatorAdapter.class);
assertThat(wrapper.supports(SampleData.class)).isTrue();
MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "test");
wrapper.validate(new SampleData(40), errors);
assertThat(errors.getErrorCount()).isEqualTo(1);
});
}
use of org.springframework.validation.MapBindingResult in project grails-core by grails.
the class RegexUrlMapping method createUrlMappingInfo.
@SuppressWarnings("unchecked")
private UrlMappingInfo createUrlMappingInfo(String uri, Matcher m) {
boolean hasOptionalExtension = urlData.hasOptionalExtension();
Map params = new HashMap();
Errors errors = new MapBindingResult(params, "urlMapping");
int groupCount = m.groupCount();
String lastGroup = null;
for (int i = 0; i < groupCount; i++) {
lastGroup = m.group(i + 1);
// if null optional.. ignore
if (i == groupCount - 1 && hasOptionalExtension) {
ConstrainedProperty cp = constraints[constraints.length - 1];
cp.validate(this, lastGroup, errors);
if (errors.hasErrors()) {
return null;
}
String propertyName = cp.getPropertyName();
if (lastGroup != null) {
if (FORMAT_PARAMETER.equals(propertyName) && lastGroup.startsWith(".")) {
lastGroup = lastGroup.substring(1);
}
}
// if the format is specified but the value is empty, ignore it
if (!(FORMAT_PARAMETER.equals(propertyName) && GrailsStringUtils.isEmpty(lastGroup))) {
params.put(propertyName, lastGroup);
}
break;
} else {
if (lastGroup == null)
continue;
int j = lastGroup.indexOf('?');
if (j > -1) {
lastGroup = lastGroup.substring(0, j);
}
if (constraints.length > i) {
ConstrainedProperty cp = constraints[i];
cp.validate(this, lastGroup, errors);
if (errors.hasErrors()) {
return null;
}
String propertyName = cp.getPropertyName();
if (FORMAT_PARAMETER.equals(propertyName) && lastGroup.startsWith(".")) {
lastGroup = lastGroup.substring(1);
}
// if the format is specified but the value is empty, ignore it
if (!(FORMAT_PARAMETER.equals(propertyName) && GrailsStringUtils.isEmpty(lastGroup))) {
params.put(propertyName, lastGroup);
}
}
}
}
for (Object key : parameterValues.keySet()) {
params.put(key, parameterValues.get(key));
}
if (controllerName == null) {
controllerName = createRuntimeConstraintEvaluator(GrailsControllerClass.CONTROLLER, constraints);
}
if (actionName == null) {
actionName = createRuntimeConstraintEvaluator(GrailsControllerClass.ACTION, constraints);
}
if (namespace == null) {
namespace = createRuntimeConstraintEvaluator(NAMESPACE, constraints);
}
if (viewName == null) {
viewName = createRuntimeConstraintEvaluator(GrailsControllerClass.VIEW, constraints);
}
if (redirectInfo == null) {
redirectInfo = createRuntimeConstraintEvaluator("redirect", constraints);
}
DefaultUrlMappingInfo info;
if (forwardURI != null && controllerName == null) {
info = new DefaultUrlMappingInfo(forwardURI, getHttpMethod(), urlData, grailsApplication);
} else if (viewName != null && controllerName == null) {
info = new DefaultUrlMappingInfo(viewName, params, urlData, grailsApplication);
} else {
info = new DefaultUrlMappingInfo(redirectInfo, controllerName, actionName, namespace, pluginName, getViewName(), getHttpMethod(), getVersion(), params, urlData, grailsApplication);
}
if (parseRequest) {
info.setParsingRequest(parseRequest);
}
return info;
}
use of org.springframework.validation.MapBindingResult in project alf.io by alfio-event.
the class AdminReservationManager method performConfirmation.
private Result<Triple<TicketReservation, List<Ticket>, PurchaseContext>> performConfirmation(String reservationId, PurchaseContext purchaseContext, TicketReservation original, Notification notification, String username, UUID subscriptionId) {
try {
var reservation = original;
if (subscriptionId != null && purchaseContext.ofType(PurchaseContextType.event)) {
if (reservation.getVatStatus() == null && purchaseContext.getVatStatus() != null) {
// set default vatStatus if not present
ticketReservationRepository.updateVatStatus(reservationId, purchaseContext.getVatStatus());
reservation = ticketReservationManager.findById(reservationId).orElseThrow();
}
var subscriptionDetails = subscriptionRepository.findSubscriptionById(subscriptionId);
var bindingResult = new MapBindingResult(new HashMap<>(), "");
boolean result = ticketReservationManager.validateAndApplySubscriptionCode(purchaseContext, reservation, Optional.of(subscriptionId), subscriptionId.toString(), subscriptionDetails.getEmail(), bindingResult);
if (!result) {
var message = bindingResult.getGlobalErrors().stream().findFirst().map(DefaultMessageSourceResolvable::getCode).orElse("Unknown error");
return Result.error(ErrorCode.custom(message, String.format("Cannot assign subscription %s to Reservation %s", subscriptionId, reservationId)));
}
reservation = ticketReservationManager.findById(reservationId).orElseThrow();
}
PaymentSpecification spec = new PaymentSpecification(reservationId, null, reservation.getFinalPriceCts(), purchaseContext, reservation.getEmail(), new CustomerName(reservation.getFullName(), reservation.getFirstName(), reservation.getLastName(), purchaseContext.mustUseFirstAndLastName()), reservation.getBillingAddress(), reservation.getCustomerReference(), LocaleUtil.forLanguageTag(reservation.getUserLanguage()), false, false, null, reservation.getVatCountryCode(), reservation.getVatNr(), reservation.getVatStatus(), false, false);
ticketReservationManager.completeReservation(spec, PaymentProxy.ADMIN, notification.isCustomer(), notification.isAttendees(), username);
return loadReservation(reservationId);
} catch (Exception e) {
return Result.error(ErrorCode.ReservationError.UPDATE_FAILED);
}
}
use of org.springframework.validation.MapBindingResult in project spring-boot by spring-projects.
the class DefaultErrorAttributesTests method extractMethodArgumentNotValidExceptionBindingResultErrors.
@Test
public void extractMethodArgumentNotValidExceptionBindingResultErrors() throws Exception {
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new MethodArgumentNotValidException(null, bindingResult);
testBindingResult(bindingResult, ex);
}
Aggregations