9

I need to validate a field in POJO, it must be min length = 2, ignoring leading and trailing whitespaces

class User {
    @NotBlank
    @Size(min = 2)
    private String name;
}

it not works for " A"

How it should be?

Romper
  • 2,009
  • 3
  • 24
  • 43
  • How about writing your own validator ? – zakaria amine Mar 21 '17 at 08:56
  • yeap it is a solution, but all code is with annotations and it is more clear with them – Romper Mar 21 '17 at 09:02
  • With hibernate validator you can create your own annotations as well – zakaria amine Mar 21 '17 at 09:06
  • ok, I thought it will be something implemented in libraries – Romper Mar 21 '17 at 09:08
  • You could use @PostLoad to provide a lifecycle method to trim. [Refer] (http://stackoverflow.com/questions/21902919/how-to-trim-white-spaces-from-char-fields-pojo-using-hibernate-and-legacy-databa). This will work as a setter. But you should provide it as a static implementation and do the post processing using a lib func. – phoenix Mar 21 '17 at 09:27

3 Answers3

19

At first Spring will use setter-method to set value of property. And for validate value Spring will get it with getter-method. That means, you can trim value in setter-method for prepare it to validation:

public class User {
    @NotBlank
    @Size(min = 2)
    private String name;

    public void setName(String value){
        this.name = value.trim();
    }

    public String getName(){
        return this.name;
    }
}
Ken Bekov
  • 13,696
  • 3
  • 36
  • 44
  • @Romper you are welcome. By the way, when you use `@Size` restriction, you can remove `@NotBlank`. Blank value will not be accepted anyway. – Ken Bekov Mar 21 '17 at 10:55
  • 4
    An empty string " " will be accepted without @NotBlank – Romper May 23 '17 at 11:20
0

Try using below method, this will take care of whitespaces. And you don't need to configure/code specific code to each field. This will be helpful when you are dealing with more number of fields. StringTrimmerEditor(true) -- it makes it to null, if it has only whitespaces.

StringTrimmerEditor(false) -- it just trims the leading and trailing whitespaces. if it has only whitespaces, just makes it empty string.

in Controller class:

@InitBinder
public void initialBinderForTrimmingSpaces(WebDataBinder webDataBinder) {
    StringTrimmerEditor stringTrimEditor = new StringTrimmerEditor(true);
    webDataBinder.registerCustomEditor(String.class, stringTrimEditor);
}
0

So a much simpler suggestion (at least in my eyes), is to create a method that checks if the trimmed length is less than the min (in this case 2) or simply check when assigning the string. It is much easier and less convoluted then creating a custom annotation. If so, set the name to the trimmed string, else do nothing.

ex:

public void setName(String str){
    if(str.trim().length() < 2)
        str = str.trim();
    this.name = str;
}

I myself was thinking this exact question, but while I did not want to have a name that was less than 2 legit characters, I did not wish to trim the actual string if it simply had a space between 2 names (ex first and last name).

And yes, I do know this is about 3 years after the question was asked.

Matt
  • 1
  • 1