Announcement Announcement Module
Collapse
No announcement yet.
Failed to convert property (String to BigDecimal) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Failed to convert property (String to BigDecimal)

    Hi

    I'm facing an error, and nothing in Google pointed how to fix it.
    I have a input field in a form that will be mapped to a BigDecimal command field. If I leave the field empty or enter a correct value, it works fine, but if I enter any invalid number (like one containing alpha characters), I get this message on <form:errors>:

    Code:
    Failed to convert property value of type [java.lang.String] to required type [java.math.BigDecimal] for property myProperty; nested exception is java.lang.NumberFormatException
    I want to capture this exception and replace it with a suitable error message, but I don't know where to capture or how to treat this. I messed around with custom editors on initBinder and also onBindAndValidate, but I see the error must be handled somewhere else.

    Any help is appreciated!
    Thanks!

    Other info: Spring 2.5.6, Spring MVC and using SimpleFormController for this form.

  • #2
    Please, I know this is some silly question to ask. I've seen people with the same problem, but never a solution. And I'm sure this is something to do with no more than half dozen lines of code, at most.

    Thanks!

    Comment


    • #3
      bumping topic... I still need this

      Comment


      • #4
        The problem is before you can validate the model object, spring tries to bind the value entered on the form to the model. This is when its throwing the error. So, I believe you will have to extend onBind method to avoid this conversion problem. I thought Custom editors should do the job though.

        Comment


        • #5
          Thanks for your reply, smrutimo.

          When the execution flow reaches onBind, the binding errors are already on the BindException object, so the handling must take place earlier in the execution flow.
          And custom editors seem unfit for this because they are not part of the form validation process and have no access to any error reporting object like BindException or Errors. The most they can do is throw an unchecked exception.

          Any other help on this, I'll be very grateful!
          Thanks again!

          Comment


          • #6
            Have you looked at this approach ?

            http://forum.springsource.org/showth...string+Integer

            Comment


            • #7
              It was not good, smrutimo. Property editors definitely do not seem fit for this, and implementing a custom tag is not a nice idea for me, as my Spring knowledge is entry level, I'm still understanding many of its details and mechanics.

              But finally, I found a solution. It feels like I'm doing it the 'rough' way, but works and is very simple.
              For anyone with a similar problem, here is the code (inside the controller):

              Code:
              	@Override
              	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
              		super.initBinder(request, binder);
              		binder.setBindingErrorProcessor(new BindingErrorProcessorImpl(binder.getBindingErrorProcessor()));
              	}
              	
              	public class BindingErrorProcessorImpl implements BindingErrorProcessor {
              
              	    private BindingErrorProcessor defaultBindingErrorProcessor;
              
              	    public BindingErrorProcessorImpl(BindingErrorProcessor defaultBindingErrorProcessor) {
              	    	defaultBindingErrorProcessor = this.defaultBindingErrorProcessor;
              	    }
              	    
              	    @Override
              	    public void processMissingFieldError(String missingField, BindingResult bindingResult) {
              	    	defaultBindingErrorProcessor.processMissingFieldError(missingField, bindingResult);
              	    }
              	    
              	    @Override
              	    public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
              	        if(ex.getPropertyChangeEvent().getPropertyName().equals("myPropertyName")) {
              	        	bindingResult.rejectValue("myPropertyName", "validation.invalid_field");
              	        } else {
              	        	defaultBindingErrorProcessor.processPropertyAccessException(ex, bindingResult);
              	        }
              	    }
              
              	}
              For discussions about the code, here is the source:
              http://stackoverflow.com/questions/6...-error-message

              Bye!

              Comment

              Working...
              X