Announcement Announcement Module
Collapse
No announcement yet.
question: parameterize spring messages for field binder errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • question: parameterize spring messages for field binder errors

    here it goes:

    My init binder method:
    Code:
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
    	{
    		DateFormat df = new SimpleDateFormat(contextUtil.getGlobalDateFormat(request.getLocale()));
    		df.setLenient(false);
    		CustomDateEditor editor = new CustomDateEditor(df, false);
    		binder.registerCustomEditor(Date.class, editor);
    	}
    The command class I use for the form has a Date property which is bound to a form:input with a certain format (mm-dd-yyyy) for Dutch locale.

    When the user enters something like 23/11/2008 the binder rejects the value because it cannot be parsed with the format mm-dd-yyyy.

    In my properties file I have the resolvable error message for dates. In English: The date you entered is incorrect, please use the format: {0}

    Notice the {0} at the end.

    When the validator is used to reject values, it's possible to inject the message with an attribute. I use something like this:
    Code:
    errors.rejectValue("client.id", "error.field.invalid.mustSelect",
    					new Object[] { new DefaultMessageSourceResolvable(
    							new String[] { "client.customer" }) }, null);
    My question is: how to I get attributes in the field errors created by the binder?


    I tried something like this (which is obviously way to much work to get something like this running), besides it didnt work because the fielderrors list is an inmutable collection and thus i cannot remove fielderrors from it:
    Code:
    protected void onBind(HttpServletRequest request, Object command, BindException errors) throws Exception
    	{
    		List<FieldError> newList = new ArrayList<FieldError>();
    		List<FieldError> removeList = new ArrayList<FieldError>();
    		List<FieldError> lst = errors.getFieldErrors();
    		for (FieldError fieldError : lst)
    		{
    			if(fieldError.isBindingFailure() && (errors.getFieldType(fieldError.getField()) == Date.class || errors.getFieldType(fieldError.getField()) == Calendar.class))
    			{
    				newList.add(new FieldError(
    						fieldError.getObjectName(), 
    						fieldError.getField(), 
    						fieldError.getRejectedValue(), 
    						fieldError.isBindingFailure(), 
    						fieldError.getCodes(), 
    						new Object[] {
    							contextUtil.getGlobalDateFormat(request.getLocale())
    						},
    						fieldError.getDefaultMessage()
    				));
    			}
    			removeList.add(fieldError);
    		}
    		lst.removeAll(removeList);
    		lst.addAll(newList);
    		
    		super.onBind(request, command, errors);
    	}

  • #2
    does anyone have any id?

    Comment

    Working...
    X