简体   繁体   中英

Spring MVC/hibernate Form validation, not returning to form

I am currently working on some HTML form validation using Hibernate and Spring MVC.

I have applied some validation to the Enity & added the code into my controller. When submitting the form to the controller with data in the incorrect format, a error page page is displayed (500). however, I want the form to be returned to the user with the error message displayed near the incorrect field.

Entity Code:

 @NotNull(message="Please enter a product")
    @Column(name="product_name")
    private String productName;

    @NotNull(message="Please enter a product code")
    @Pattern(regexp="([A-Z]{2,4})-([0-9]{5})|", message="Incorrect format")
    @Column(name="product_code")
    private String productCode;

Controller Code:

    @GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {

    // create model attribute to bind form data
    QaRaised theProduct = new QaRaised();

    theModel.addAttribute("product", theProduct);

    return "product-form";
}

@PostMapping("/saveProduct")
public String saveProduct(@Valid @ModelAttribute("product") QaRaised theProduct, BindingResult bindingResult) {

    qaRaisedService.saveProduct(theProduct);
    if (bindingResult.hasErrors()) {
        return "product-form";
    }

    return "redirect:/products/qaraised";
}

Stack trace from Error:

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.sonya.spring.entity.QaRaised] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[ConstraintViolationImpl{interpolatedMessage='Incorrect format', propertyPath=productCode, rootBeanClass=class com.sonya.spring.entity.QaRaised, messageTemplate='Incorrect format'}

Form code:

  <div class="form-group">
<label for="InputPC">Product Code:</label>
<form:input required="true" type="text"  title="Product Code" path="productCode" class="form-control" id="productCodeInput" placeholder="Enter Product Code" commandName="productCode"/>
<form:errors path="productCode"/>

The validation here is working correctly, I simply want to redirect the user back to the form. Can anyone see anything I am missing or point me in the right direction?

Cheers, Danny

This is an old question but I actually had the exact same issue with the same exact coding problem you had, so in case anyone else comes across this...

@want2learn is right - you shouldn't try to save the data before making sure it is valid. Otherwise, the validator will throw an exception. Just move your saving logic down:

@PostMapping("/saveProduct")
public String saveProduct(@Valid @ModelAttribute("product") QaRaised theProduct, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        return "product-form";
    }
    qaRaisedService.saveProduct(theProduct);
    return "redirect:/products/qaraised";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM