Announcement Announcement Module
Collapse
No announcement yet.
CustomDateEditor errors and friendly Validation-style error messages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CustomDateEditor errors and friendly Validation-style error messages

    Hello all

    Running Spring 2.5.6 inside of Oracle 10g OC4J + Oracle Portal I've got a CustomDateEditor with a SimpleDateFormat inside it, added to my controller in the initBinder method of my SimpleFormController (the portlet MVC version of all this applies here).

    I've also got some date validation in a Validator class for the command class that's used by the controller. It has some basic rules like end date must be after start date, start date cannot be in the past, etc. The validator works great for the simple validation errors.

    The problem that I experience is if the CustomDateEditor encounters a date formatting error, the text that is placed in the Error list in the form:errors tage and appears on the user's screen is much less than friendly, e.g.

    Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property newAlert.startDate; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "helloworld"
    I would like to make this error message much more friendly to the user, e.g. the message just to say something nicer like 'Invalid date format: "helloworld", please use dd/MM/yyyy' without the rest of the stack trace attached.

    Is there a way to control how the error from the CustomDateEditor appears to the user, like the way I can control the Validation messages? I read through the various chapters e.g. on Validation and Databinding, and the Web MVC chapter, and the Portlet MVC chapter, and searched the forums here but I can't locate anything specifically relevant to this particular situation.

    I assume that even if I implement my own version of the CustomDateEditor which throws a slightly nicer error message that the part of the spring framework that calls it will add the stack trace to the Error messages anyway, as it did above? (The CustomDateEditor throws the IllegalArgumentException with the message 'Could not parse date: Unparseable date: "helloworld"', therefore the rest of the message displayed above is added to it by the part of the framework that invoked this).

    I don't want to use Spring's exception handling framework either because I still want the error to display as if it were a validation error (which in a way, it is) rather than routing the user to an error page.

    Even if I could pare the error message displayed down to just the 'Could not parse date: Unparseable date: "helloworld"' it would be a win.

    Any help or pointers appreciated.

    thanks
    scot

  • #2
    It works in EXACTLY the same way as all the other validation messages. Check the DefaultMessageCodeResolver for the list of registered error codes.

    Comment


    • #3
      Hi Scot,
      You can do it by using <form:errors> tag in ur jsp and set the path attribute of the tag as the property that you are using for this date.
      This <form:errors> will be used only for displaying the error.
      Besides map this error in a property file and you have to load the property file in ur xml file(the xml has to stay loaded in container during runtime).
      If still not clear mention please.
      Hope this helps,
      Cheers,
      Amit

      Comment


      • #4
        Originally posted by Marten Deinum View Post
        It works in EXACTLY the same way as all the other validation messages. Check the DefaultMessageCodeResolver for the list of registered error codes.
        Excuse my ignorance but I don't understand then how the PortletDataRequestBinder associates the error from the CustomDateEditor to the "errorCode" intercepted by the DefaultMessageCodesResolver?

        eg in initBinder;

        Code:
        protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) 
        throws Exception {
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
            super.initBinder(request, binder);    
        }
        As opposed to in the Validator implementation where I control what the error code is at the time of the population of the Error object;

        Code:
        public void validate(Object target, Errors errors) {
            ValidationUtils.rejectIfEmpty(errors, "userName", "userName.empty");
            // ...
        }
        All of which appears to end up (eventually) in AbstractBindingResult.resolveMessageCodes :

        Code:
        public String[] resolveMessageCodes(String errorCode, String field) {
            String fixedField = fixedField(field);
            Class fieldType = getFieldType(fixedField);
            return getMessageCodesResolver().resolveMessageCodes(errorCode, getObjectName(), fixedField, fieldType);
        }
        At which point the DefaultMessageCodeResolver is allowed to do its thing.

        It's what is in-between the initBind() - which doesn't have the errorCode specified (by me at least) and the DefaultMessageCodeResolver being the part I don't fully understand how I control that, or what its default value might be.

        (I'm unable to find out at run time what the default value is as I cannot hook a debugger up to the Oracle Portal server - a very nasty set of firewall rules stops me, and my rather incomplete knowledge of this).

        Amit: I can bind the error to the *field* on the JSP just great (i.e. show the message text next to the field that's wrong), what is eluding me is control of the errorCode from the CustomDateEditor - I'm just getting its exception message.

        Comment


        • #5
          what is the error you have mapped in ur property file for this?
          and can u show ur jsp code segment where you are showing the error for this field?

          Comment


          • #6
            Originally posted by amit.goenka View Post
            what is the error you have mapped in ur property file for this?
            and can u show ur jsp code segment where you are showing the error for this field?
            I haven't mapped one 'cos I've got no idea what code to use. As opposed to "username.empty" for example.

            This is the JSP code;

            Code:
             <tr>
                    <th>alert start (dd/mm/yyyy)</th>
                    <td><form:input path="newAlert.startDate" size="10" maxlength="10" /></td>
                    <td class="PortletText3"><form:errors path="newAlert.startDate" cssStyle="color:red"/></td>
                </tr>
            But I don't understand why this is significant. If I input a bad date (e.g. "helloworld") the exception message from the CustomDateEditor appears right there at the form:errors tag location for the relevant field. The issue is the content of that message (i.e. what control I have over the errorCode the CustomDateEditor produces) not whether it's bound to the field or not.

            On the other hand if I leave the field empty it gets to my Validator implementation and at that point, I have complete control over the errorCode and can make it give me a user-sensible error message (e.g. "Start Date is a required field").

            thanks
            scot

            Comment


            • #7
              in ur property file add the following entry:
              typeMismatch.startDate=whatever error message you want
              and c wat happens

              Comment


              • #8
                Thank you, that is exactly the trick.

                I see now that this is the example documented at the top of the DefaultMessageCodeResolver but it doesn't appear to be documented in the other end of the equation e.g. in the initBinder() method of SimpleFormController or in the documentation about the Custom Editors.

                I was trying to find the way to set the errorCode for the custom editor rather than using the default code, which is what threw me for a loop.

                Anyway, thanks.

                Comment

                Working...
                X