Search in sources :

Example 31 with BindingResult

use of org.springframework.validation.BindingResult in project motech by motech.

the class BootstrapControllerTest method shouldAddErrorsOnValidationFailure.

@Test
public void shouldAddErrorsOnValidationFailure() throws Exception {
    when(messageBrokerTestService.pingBroker("tcp://localhost:61616")).thenReturn(true);
    BindingResult bindingResult = mock(BindingResult.class);
    when(bindingResult.hasErrors()).thenReturn(true);
    when(bindingResult.getAllErrors()).thenReturn(Arrays.asList(new ObjectError("sqlUrl", new String[] { "server.dbUrl.error" }, null, null)));
    BootstrapConfigForm bootstrapConfigForm = new BootstrapConfigForm();
    bootstrapConfigForm.setSqlUrl("http://www.dburl.com");
    ModelAndView actualView = bootstrapController.submitForm(bootstrapConfigForm, bindingResult, request);
    assertThat(actualView.getViewName(), is("bootstrapconfig"));
    assertThat((String) ((List) actualView.getModel().get("errors")).get(0), is("server.dbUrl.error"));
}
Also used : BindingResult(org.springframework.validation.BindingResult) ObjectError(org.springframework.validation.ObjectError) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with BindingResult

use of org.springframework.validation.BindingResult in project FuryViewer by TheDoctor-95.

the class ExceptionTranslator method handleMethodArgumentNotValid.

@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream().map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getCode())).collect(Collectors.toList());
    Problem problem = Problem.builder().withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE).withTitle("Method argument not valid").withStatus(defaultConstraintViolationStatus()).with("message", ErrorConstants.ERR_VALIDATION).with("fieldErrors", fieldErrors).build();
    return create(ex, problem, request);
}
Also used : ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) BindingResult(org.springframework.validation.BindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ProblemHandling(org.zalando.problem.spring.web.advice.ProblemHandling) Collectors(java.util.stream.Collectors) DefaultProblem(org.zalando.problem.DefaultProblem) HeaderUtil(com.furyviewer.web.rest.util.HeaderUtil) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Problem(org.zalando.problem.Problem) ProblemBuilder(org.zalando.problem.ProblemBuilder) Status(org.zalando.problem.Status) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntity(org.springframework.http.ResponseEntity) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BindingResult(org.springframework.validation.BindingResult) DefaultProblem(org.zalando.problem.DefaultProblem) Problem(org.zalando.problem.Problem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)

Example 33 with BindingResult

use of org.springframework.validation.BindingResult in project dubion by valsamiq.

the class ExceptionTranslator method handleMethodArgumentNotValid.

@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream().map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getCode())).collect(Collectors.toList());
    Problem problem = Problem.builder().withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE).withTitle("Method argument not valid").withStatus(defaultConstraintViolationStatus()).with("message", ErrorConstants.ERR_VALIDATION).with("fieldErrors", fieldErrors).build();
    return create(ex, problem, request);
}
Also used : ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) BindingResult(org.springframework.validation.BindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ProblemHandling(org.zalando.problem.spring.web.advice.ProblemHandling) Collectors(java.util.stream.Collectors) DefaultProblem(org.zalando.problem.DefaultProblem) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) HeaderUtil(com.dubion.web.rest.util.HeaderUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Problem(org.zalando.problem.Problem) ProblemBuilder(org.zalando.problem.ProblemBuilder) Status(org.zalando.problem.Status) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntity(org.springframework.http.ResponseEntity) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BindingResult(org.springframework.validation.BindingResult) DefaultProblem(org.zalando.problem.DefaultProblem) Problem(org.zalando.problem.Problem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)

Example 34 with BindingResult

use of org.springframework.validation.BindingResult in project BroadleafCommerce by BroadleafCommerce.

the class RegisterCustomerControllerTest method createCustomerFromController.

@Test(groups = "createCustomerFromController", dataProvider = "setupCustomerControllerData", dataProviderClass = RegisterCustomerDataProvider.class, enabled = false)
@Transactional
@Rollback(false)
public void createCustomerFromController(RegisterCustomerForm registerCustomer) {
    BindingResult errors = new BeanPropertyBindingResult(registerCustomer, "registerCustomer");
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    registerCustomerController.registerCustomer(registerCustomer, errors, request, response);
    assert (errors.getErrorCount() == 0);
    Customer customerFromDb = customerService.readCustomerByUsername(registerCustomer.getCustomer().getUsername());
    assert (customerFromDb != null);
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Customer(org.broadleafcommerce.profile.core.domain.Customer) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.testng.annotations.Test) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 35 with BindingResult

use of org.springframework.validation.BindingResult in project jprime by bgjug.

the class TicketsController method register.

/**
 * User submitted the form.
 */
@Transactional
@RequestMapping(value = "/tickets/epay", method = RequestMethod.POST)
public String register(Model model, @Valid final Registrant registrant, BindingResult bindingResult, HttpServletRequest request) throws Exception {
    boolean invalidCaptcha = false;
    if (registrant.getCaptcha() == null || !registrant.getCaptcha().equals(request.getSession().getAttribute(CaptchaController.SESSION_PARAM_CAPTCHA_IMAGE))) {
        bindingResult.rejectValue("captcha", "invalid");
        invalidCaptcha = true;
    }
    if (bindingResult.hasErrors() || invalidCaptcha) {
        return TICKETS_REGISTER_JSP;
    }
    // check empty users, server side validation
    List<Visitor> toBeRemoved = registrant.getVisitors().stream().filter(v -> v.getEmail() == null || v.getEmail().isEmpty() || v.getName() == null || v.getName().isEmpty()).collect(Collectors.toList());
    registrant.getVisitors().removeAll(toBeRemoved);
    registrant.getVisitors().forEach(visitor -> visitor.setStatus(VisitorStatus.REQUESTING));
    if (!registrant.isCompany()) {
        handlePersonalRegistrant(registrant);
    }
    registrant.setPaymentType(Registrant.PaymentType.BANK_TRANSFER);
    Registrant savedRegistrant = registrantFacade.save(registrant);
    model.addAttribute("tags", userFacade.findAllTags());
    InvoiceData invoiceData = buildInvoiceData(savedRegistrant);
    byte[] pdf = invoiceExporter.exportInvoice(invoiceData, registrant.isCompany());
    sendPDF(savedRegistrant, generatePdfFilename(registrant, invoiceData.getSinglePriceWithVAT()), pdf);
    return result("ok", model);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) MessagingException(javax.mail.MessagingException) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) BindingResult(org.springframework.validation.BindingResult) MailService(site.facade.MailService) Controller(org.springframework.stereotype.Controller) InvoiceExporter(site.controller.invoice.InvoiceExporter) Valid(javax.validation.Valid) HttpServletRequest(javax.servlet.http.HttpServletRequest) Model(org.springframework.ui.Model) Calendar(java.util.Calendar) Visitor(site.model.Visitor) Qualifier(org.springframework.beans.factory.annotation.Qualifier) UserService(site.facade.UserService) Transactional(javax.transaction.Transactional) Registrant(site.model.Registrant) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) InvoiceData(site.controller.invoice.InvoiceData) List(java.util.List) Logger(org.apache.logging.log4j.Logger) RegistrantService(site.facade.RegistrantService) LogManager(org.apache.logging.log4j.LogManager) VisitorStatus(site.model.VisitorStatus) Visitor(site.model.Visitor) InvoiceData(site.controller.invoice.InvoiceData) Registrant(site.model.Registrant) Transactional(javax.transaction.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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