javax validation size trim whitespace
Asked Answered
I

3

10

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?

Isomeric answered 21/3, 2017 at 8:39 Comment(5)
How about writing your own validator ?Brennabrennan
yeap it is a solution, but all code is with annotations and it is more clear with themIsomeric
With hibernate validator you can create your own annotations as wellBrennabrennan
ok, I thought it will be something implemented in librariesIsomeric
You could use @PostLoad to provide a lifecycle method to trim. [Refer] (#21903419). This will work as a setter. But you should provide it as a static implementation and do the post processing using a lib func.Cruce
F
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;
    }
}
Flickinger answered 21/3, 2017 at 10:36 Comment(2)
@Isomeric you are welcome. By the way, when you use @Size restriction, you can remove @NotBlank. Blank value will not be accepted anyway.Flickinger
An empty string " " will be accepted without @NotBlankIsomeric
B
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);
}
Bellow answered 1/12, 2019 at 5:26 Comment(0)
H
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.

Hinkel answered 7/7, 2020 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.