1

I have a custom domain class with a single constructor that takes a String, as well as a toString() method. The constructor decodes the input string, performs validations on it and throws IllegalArgumentException if invalid.

I want to bind directly to this field, as described here: http://blog.springsource.org/2009/11/17/spring-3-type-conversion-and-validation/ (see 'Convention Over Configuration' section).

That is working fine & I am displaying the error message resolved by Spring (typeMismatch on barcodeInfo).

I know that I can customize this error message using a messageSource entry, e.g.

typeMismatch.barcodeInfo=Invalid format

However, the error message that I want to display isn't always the same, it depends on the value of the input string. Hence, I want to display the error message that I originally used in the IllegalArgumentException that I threw from the constructor. Is this possible?

I am specifically looking for a solution which will work with Spring WebFlow.

2 Answers 2

1

You might want to check BindingErrorProcessor used by WebDataBinder. There you can implement your own custom logic for translating exceptions to validation errors.


Notes:

  • You should implement your own exception (to be able to distinguish it from IllegalArgumentException thorwn by other components).
  • You can initialize WebDataBinder with your custom BindingErrorProcessor within your @InitBinder method (or set specific WebBindingInitializer to your handler adapter).
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer - I updated the answer to specify that I am specifically looking for a solution for Spring WebFlow. Unfortunately, after more investigation, I don't think that it is possible. Hence, I'm accepting this answer, as it answers the initial question.
1

As Pavel mentioned in his answer, you can achieve this by implementing BindingErrorProcessor.

It should look like this:

...
import org.springframework.validation.DefaultBindingErrorProcessor;
...

@Controller
public class YourController {

  ...

  @InitBinder
  public void initBinder(WebDataBinder binder) {
    binder.setBindingErrorProcessor(new DefaultBindingErrorProcessor() {
      @Override
      public void processPropertyAccessException(
          PropertyAccessException ex, BindingResult bindingResult) {
        if (ex.getPropertyName().equals("fieldInQuestion")) {
          Throwable cause = ex.getMostSpecificCause();

          FieldError fieldError;
          fieldError = new FieldError(
            bindingResult.getObjectName(),
            "fieldInQuestion",
            cause.getMessage());

          bindingResult.addError(fieldError);
        } else {
          super.processPropertyAccessException(ex, bindingResult);
        }
      }
    });
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.