Search code examples
javaspring-mvcvalidationspring-annotations

SpringMVC Annotation Validation - DataBinding data type


When using custom validation under this scenario:

if(officer.getBadgeId() == 0){

        errors.rejectValue("badgeId", "badgeId.required");
}

if the POJO/Model officer has a datatype of int for the badgeId can a string be set as the error message for the attribute? Or the datatype of the attribute does not matter when doing validation and returning error messages to the view.

If it is an issue how do you handle this scenario in terms or returning an error message to the user from custom validation


Solution

  • Error message will be of type String only.

    void rejectValue(String field, String errorCode, String defaultMessage)
    Reject the given field of the current object, using the given error description.

    Spring doc link.