0

This question has been asked a lot of times, even after going through all the solutions i wansn't able to get hibernate validator working.

Controller class:-

@RequestMapping(value={"/setReg"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView setRegistration( @Valid @ModelAttribute("userDetails") UserDetails userDetails,BindingResult bindingResult){
logger.info("setRegistration :Entry");
if(bindingResult.hasErrors()){
logger.info("binding success"); 
logger.info(userDetails.getUser_first_name());
logger.info("validation not working");
}
else{
logger.info("binding failure"); 
}
logger.info("setRegistration :Exit");
return null;    
}

servlet-context:-

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
        <context:component-scan base-package="com.xmith.services"/>
        <context:component-scan base-package="com.xmith.dao"/>
        <context:component-scan base-package="com.xmith.models"/>
        <context:component-scan base-package="com.xmith.sweb" />
    <context:annotation-config/>

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

dependency:-

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>

validation class:-

public class UserDetails {
private String user_id;
@Size(min=3,max=7,message="user first name must have max length 7")
private String user_first_name;
private String user_last_name;
private String user_age;
private String user_email;
private String user_password;

Note:- in above code i am trying to validate "user_first_name"(min=3,max=7,message="user first name must have max length 7"), however when i enter input more than 7 it still sets "user_first_name".

output:---

17:33:12.399 [http-bio-8080-exec-1] DEBUG org.hibernate.validator.resourceloading.PlatformResourceBundleLocator - ValidationMessages not found. 17:33:12.401 [http-bio-8080-exec-1] DEBUG org.hibernate.validator.resourceloading.PlatformResourceBundleLocator - ContributorValidationMessages not found. 17:33:12.403 [http-bio-8080-exec-1] DEBUG org.hibernate.validator.resourceloading.PlatformResourceBundleLocator - org.hibernate.validator.ValidationMessages found. 17:33:12.417 [http-bio-8080-exec-1] INFO com.xmith.sweb.HomeController - setRegistration :Entry 17:33:12.418 [http-bio-8080-exec-1] INFO com.xmith.sweb.HomeController - binding success 17:33:12.418 [http-bio-8080-exec-1] INFO com.xmith.sweb.HomeController - qwertyuiiii 17:33:12.418 [http-bio-8080-exec-1] INFO com.xmith.sweb.HomeController - validation not working 17:33:12.418 [http-bio-8080-exec-1] INFO com.xmith.sweb.HomeController - setRegistration :Exit

What am i missing here?

2
  • 2
    You're printing "binding success" and "validation not working" inside if(bindingResult.hasErrors()). It's just your boolean logic which is wrong.
    – JB Nizet
    Jul 15, 2018 at 12:37
  • thank. Such a lame mistake, my bad.
    – smith
    Jul 15, 2018 at 12:41

1 Answer 1

0

my boolean logic was wrong as pointed by – JB Nizet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.