2

My code looks like this

@RequestMapping(value = "/test2Form", method = RequestMethod.POST)
public String testForm(@ModelAttribute("someBean") SomeBean someBean, BindingResult result) {
    BindException errors = new BindException(someBean, "someBean");
    someValidator.validate(someBean, errors);

    for (FieldError error : errors.getFieldErrors()) {
        result.addError(new FieldError("someBean", error.getField(), error.getCode()));
    }

    // Field error
    if (result.hasErrors()) {
        return "test/test1";
    }

    return "index";
}

public class SomeBean{
    private Integer someInteger;
    // getter and setter
}

This works fine until the user changes the value via HTML to a non Integer value, e.g. like this: http://www.pichost.de/image/9IN

In this case a type mismatch occurs (something like Failed to convert property value of type java.lang.String to required type java.lang.Integer for property someInteger; nested exception is java.lang.NumberFormatException). I don't want to display this generated error message. Instead, I want to display only my error messages. Is this possible?

My JSP looks like this:

<form:form action="test2Form.htm" commandName="someBean" method="post">
    <form:select path="someInteger">
        <option value="1">Value 1</option>
        <option value="2">Value 2</option>
        <option value="3">Value 3</option>
    </form:select>
    <form:errors path="someInteger" element="div"/>

    <input type="submit" value="Submit">
</form:form>
baris1892
  • 981
  • 1
  • 16
  • 29
  • What do you mean _handle this properly_? Spring has already handled it for you within the `BindingResult`. – Sotirios Delimanolis Jun 17 '15 at 14:51
  • That's true - but I don't want to display the error message generated from Spring (I want to display only the error messages I've added via `someValidator`). Is this possible? – baris1892 Jun 17 '15 at 14:53
  • You'll need to employ ExceptionHandling in your Java code to avoid these errors. Then you can offer the user your own custom error messages. – smoggers Jun 17 '15 at 15:07
  • And what are you going to show? Binding didn't happen because it wasn't possible, so what are you going to show? Also it is quite a contraption you created, why are you constructing your own `BindingResult`? It would be easier to add `@Valid` to the method argument and use an `@InitBinder` method to set the validator, that way you save some code and use the default mechanism. – M. Deinum Jun 17 '15 at 15:40
  • @M. Deinum: I think the current output with `Failed to convert property value...` should not be displayed to the user, so my thought was to hide this message and display something else like 'choose a valid value'. Isn't that the correct way to do this? – baris1892 Jun 17 '15 at 16:00
  • 1
    You can easily override the `Failed to convert ...` messages to anything you want. Take a look at [this answer](http://stackoverflow.com/a/30851052/1291150) for details. – Bohuslav Burghardt Jun 17 '15 at 16:47
  • The message you see is a default message. Register a message source and add a file containing another message then the default. So no that isn't the correct way to handle that. – M. Deinum Jun 18 '15 at 06:16
  • @Bohuslav Burghardt and M. Deinum: Thanks for the info! I think the link Bohuslav Burghardt provided will help me doing this. – baris1892 Jun 18 '15 at 10:02

1 Answers1

2

You can add an exception handler to you controller to handle NumberFormatExcpetion.

@ExceptionHandler(NumberFormatException.class) 
public String handleNumberFormatException(NumberFormatException e...) {
//add error for view here
}
6ton
  • 4,174
  • 1
  • 22
  • 37