0

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

devdar
  • 5,564
  • 28
  • 100
  • 153

2 Answers2

3

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.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • 1
    You mean `rejectValue(String field, String errorCode)` I think –  Sep 02 '12 at 04:51
  • @Ajinkya yes i was wondering with the errorCode being type String if it would have affected the class attribute type of type int in some way and throw an error – devdar Sep 02 '12 at 04:55
  • 1
    @dev_darin: Error message will be of type String irrespective of whatever the type of field it belongs to. – Ajinkya Sep 02 '12 at 05:02
0

This is an example from: http://alasdoo.com/2011/07/data-validation-conversion-basics-custom-error-messages-in-spring-mvc/

if (user.getBirthYear()!=null)
            if ( user.getBirthYear() <MINI_YEAR || user.getBirthYear() >MAX_YEAR)
                e.rejectValue( "birthYear", "user.birthYear.outOfInterval");
    }

Here you can see birthYear being a date type is returning an error message that is of type String stored in a message.properties file.

devdar
  • 5,564
  • 28
  • 100
  • 153