简体   繁体   中英

@NotNull and @size Spring Validation doesn't work

Problem was solved: The problem is Hibernate Validator 7, which is a JakartaEE implementation and not a JavaEE implementation of the validation API. Use Hibernate Validator 6 which is compatible with Spring 5. For JakartaEE you need Spring 6 which is still in development at this moment. – M. Deinum

So am doing spring and hibernate course and got to @NotNull and @size chapter. I implemented hibernate jar files 1,2,3 4,5,6,7 and I don't get any errors in code, but @NotNull and @Size does not work, There is another post about this problem and I saw no solution there so I had to make new post.

Hibernate validator I implement is hibernate-validator-7.0.3.Final and spring am using is spring-framework-5.3.9

   package com.luv2code.springdemo.mvc;
    
    import jakarta.validation.constraints.NotNull;
    import jakarta.validation.constraints.Size;
    
    public class Customer {
        
        private String firstName;
        
        @Size(min = 1, message ="is required")
        @NotNull( message ="is required")
        private String lastName;
    
        
        public Customer() {
            
        }
    
        public String getFirstName() {
            return firstName;
        }
    
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
    
        public String getLastName() {
            return lastName;
        }
    
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        
    }

/

<%@ taglib prefix= "form" uri="http://www.springframework.org/tags/form" %>

<html>
<head>

<title> Customer registration form</title>

<style>
    .error{color:red}
</style>


</head>
<body>
        <form:form action ="processForm" modelAttribute = "customer">
        
        
            First name: <form:input path="firstName" />
            
            <br><br>
            
            Last name: <form:input path="lastName"/>
            <form:errors path= "lastName" cssClass = "error"/>
            
            <br><br>
            
            <input type= "submit" value ="Submit" />
        
        </form:form>
    
    
</body>

</html>

/

package com.luv2code.springdemo.mvc;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.validation.Valid;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    
    @RequestMapping("/showForm")
    public String showForm(Model theModel) {
        
        theModel.addAttribute("customer", new Customer());
        
        return "customer-form";
    }
    
    @RequestMapping("/processForm")
    public String processForm(@Valid @ModelAttribute("customer") Customer theCustomer,
            BindingResult theBindingResult) {
        
        if(theBindingResult.hasErrors()) {
            
            return "customer-form";
        } else {
            return "customer-confirmation";
        }
    }
    
}

try to use @NotNull @NotBlank @Size in this sequence

If you want spaces as valid string then use @NotEmpty

I had similar issue.

Replacing:

@jakarta.validation.constraints.NotNull

with

@org.hibernate.validator.constraints.NotBlank

and

@jakarta.validation.constraints.Size

with

@org.hibernate.validator.constraints.Length

helped me

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