Announcement Announcement Module
No announcement yet.
Repopulating Failed form values Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Repopulating Failed form values

    I was looking at JSF and Spring and comparing the spring:bind with the JSF state management.

    I was curious about how Spring maintains form values to repopulate the html form upon a failed validation.

    For example:

    I have a date field and the user enters the value ‘01/02/3’. The submit button is pressed and the request is routed to the controller. Validation is called and fails. So, the controller directs back to the form page with errors. Does spring maintain the failed user input value so that the date field is populated with the ‘01/02/3’ (which is desirable) or does it look into the model object for a value and populate it as empty? Looking at the docs I was curious as to what “status” is and how and where it is created and maintained. All I have seen in the Controller is the ability to call BindUtils.bindAndValidate which returns a BindException that then can be used to extract the model from and passed back in the returning ModelAndView object. Is that where the status is created? Sorry if this is a lame question. But, I’m trying to understand the internal workings so that I can better understand the end behavior.

    --- example from docs ---

    <spring:bind path="">
    ## render a form field, containing the value, the expression
    Name: <input
    value="<c:out value="${status.value}"/>"
    name="<c:out value="${status.expression}"/>">
    ## if there are error codes, display them!
    <c:if test="${status.error}">
    Error codes:
    <c:forEach items="${status.errorMessages}" var"error">
    <c:out value="${error}"/>

  • #2
    Re: Repopulating Failed form values

    Originally posted by phasews
    I was curious about how Spring maintains form values to repopulate the html form upon a failed validation.
    If you turn on sessionForm for form controller and validation fails, by default the command object will keeps its current values (on which validation failed) and using the code you showed in your message, the form will be populated using those.

    The status object (BindStatus) doesn't really keep any state, it's an object created by the custom spring:bind tag and also used in other form simplification macros like the ones for FreeMarker and Velocity. It's meant to serve as conversion utility using property editors and to see if there are errors available for the property on which you've just doen spring:bind (errors are extracted from the model, more specifically from the Errors object --> I believe BindException extends Errors)



    • #3
      So then the failed date value would NOT be passed back to the form? Instead it would simply leave a blank form field with an error? I hope that is not the case.


      • #4
        Err, no. The failed date value will be passed back to the form, as long as it's parseable to a Date. If binding results in a IllegalArgumentException (because the property editor could not transform the string into a date), the property of the command object will stay empty.

        class Bla {
        public void setDate(Date d)
        public Date getDate();

        form input: fjdsklfjsdkl
        result: empty

        form input: 2004/04/31 (validation fails because out of bounds of something)
        result: 2004/04/31



        • #5
          My understanding of the <spring:bind> tag and the ${status.value} stuff is to allow Spring to perform automatic binding while still allowing the user to edit mis-typed information when validation fails. Consequently, when validation errors exist (including TypeMismatch) and the field value is set with ${status.value} within <spring:bind> tags, you will get the value exactly as it was typed by the user (regardless of correctness) when the form is re-displayed for the user to correct the errors.