Tuesday, 23 April 2013

Java, Spring: creating error messages in Spring - reject() vs rejectValue()

When to use errors.reject() or errors.rejectValue()??

"The difference between rejectValue and reject is that rejectValue
always has to be about a field, hence it is a FieldError. The reject
method generates global errors."

Be careful with your calls because .reject() and rejectValue() both
have two string parameters. If your call is meant to be for
.rejectValue() but call .reject() instead, it will not break but also
won't work!


REF:
http://forum.springsource.org/archive/index.php/t-34649.html


From

org/springframework/spring-context/3.0.5.RELEASE/spring-context-3.0.5.RELEASE-sources.jar!/org/springframework/validation/Errors.java

Errors.java


/**
* Register a field error for the specified field of the current object
* (respecting the current nested path, if any), using the given error
* description.
* <p>The field name may be <code>null</code> or empty String to indicate
* the current object itself rather than a field of it. This may result
* in a corresponding field error within the nested object graph or a
* global error if the current object is the top object.
* @param field the field name (may be <code>null</code> or empty String)
* @param errorCode error code, interpretable as a message key
* @see #getNestedPath()
*/
void rejectValue(String field, String errorCode);




/**
* Register a global error for the entire target object,
* using the given error description.
* @param errorCode error code, interpretable as a message key
*/
void reject(String errorCode);

/**
* Register a global error for the entire target object,
* using the given error description.
* @param errorCode error code, interpretable as a message key
* @param defaultMessage fallback default message
*/
void reject(String errorCode, String defaultMessage);

No comments: