Home > other >  @NotNull and @size Spring Validation doesn't work
@NotNull and @size Spring Validation doesn't work

Time:03-07

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";
        }
    }
    
}

CodePudding user response:

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

If you want spaces as valid string then use @NotEmpty

CodePudding user response:

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

  • Related