Search in sources :

Example 16 with MapBindingResult

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());
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException) MapBindingResult(org.springframework.validation.MapBindingResult) ServletException(jakarta.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.jupiter.api.Test)

Example 17 with MapBindingResult

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);
    });
}
Also used : MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.jupiter.api.Test)

Example 18 with MapBindingResult

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;
}
Also used : Errors(org.springframework.validation.Errors) MapBindingResult(org.springframework.validation.MapBindingResult) ConstrainedProperty(grails.gorm.validation.ConstrainedProperty)

Example 19 with MapBindingResult

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);
    }
}
Also used : MapBindingResult(org.springframework.validation.MapBindingResult) PaymentSpecification(alfio.manager.payment.PaymentSpecification) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DuplicateReferenceException(alfio.manager.support.DuplicateReferenceException)

Example 20 with MapBindingResult

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);
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MapBindingResult(org.springframework.validation.MapBindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletException(javax.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.Test)

Aggregations

MapBindingResult (org.springframework.validation.MapBindingResult)40 Test (org.junit.Test)22 ObjectError (org.springframework.validation.ObjectError)12 BindingResult (org.springframework.validation.BindingResult)11 HashMap (java.util.HashMap)10 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)9 Test (org.junit.jupiter.api.Test)6 Relationship (org.openmrs.Relationship)6 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)6 Method (java.lang.reflect.Method)5 GuiFragmentRequestBody (org.entando.entando.web.guifragment.model.GuiFragmentRequestBody)5 MethodParameter (org.springframework.core.MethodParameter)5 BindException (org.springframework.validation.BindException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ServletException (jakarta.servlet.ServletException)3 RestAccessControl (org.entando.entando.web.common.annotation.RestAccessControl)3 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)3 ResponseEntity (org.springframework.http.ResponseEntity)3 FieldError (org.springframework.validation.FieldError)3 WebExchangeBindException (org.springframework.web.bind.support.WebExchangeBindException)3