Results 1 to 2 of 2

Thread: question: parameterize spring messages for field binder errors

  1. #1
    Join Date
    Jul 2008
    Location
    Almere
    Posts
    16

    Default 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. #2
    Join Date
    Jul 2008
    Location
    Almere
    Posts
    16

    Default

    does anyone have any id?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •