Resolve issue on white space in validation(Spring validation)




In the above form we need to fill all required fields to submit the form. In some case we able to by pass the form by using white spaces. But the application could not able to allow the user to submit with white space.

For resolve this issue we can use @InitBinder in spring. This @InitBinder will


  • Pre-process every string form data 
  • Remove leading and trailing white space
  • If string only has white space ... trim it to null


For use this @InitBinder we need to initBinder method in the Controller.

@InitBinder
public void initBinder(WebDataBinder dataBinder) {

StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);

}

By using this we can resolve the issue



Comments

Popular posts from this blog

Nginx

AWS Configuration For RDS(postgres),ElastiCache(Redis) with ElasticBean

Use @Initbinder in Spring MVC

How to read Dates with Hibernate

CSRF Protection using Synchronizer Tokens

CSRF Protection using Double Submitted Cookies

Add Logging Messages in Spring 5.1 - All Java Config Version

The TRUE difference between [] and {{}} bindings in Angular

Hibernate and Primary Keys