Handle case where text is blank in the enum parser
This is to avoid calling the hints provider when it is not necessary and to better handle the case when no text is present (error should be thrown)
This commit is contained in:
@@ -41,6 +41,12 @@ public class EnumValueParser implements ValueParser {
|
||||
}
|
||||
|
||||
public Object parse(String str) {
|
||||
// IMPORTANT: check the text FIRST before fetching values
|
||||
// from the hints provider, as the hints provider may be expensive when resolving values
|
||||
if (!StringUtil.hasText(str)) {
|
||||
throw new IllegalArgumentException(createBlankTextErrorMessage());
|
||||
}
|
||||
|
||||
Collection<String> values = this.values.get();
|
||||
//If values is not known (null) then just assume the str is acceptable.
|
||||
if (values==null || values.contains(str)) {
|
||||
@@ -50,6 +56,10 @@ public class EnumValueParser implements ValueParser {
|
||||
}
|
||||
}
|
||||
|
||||
protected String createBlankTextErrorMessage() {
|
||||
return "'"+typeName+"'" + " cannot be blank.";
|
||||
}
|
||||
|
||||
protected String createErrorMessage(String parseString, Collection<String> values2) {
|
||||
return "'"+parseString+"' is not valid for Enum '"+typeName+"'. Valid values are: "+values;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user