Results 1 to 5 of 5

Thread: Repopulating Failed form values

  1. #1
    Join Date
    Aug 2004
    Posts
    5

    Default 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="Company.name">
    ## render a form field, containing the value, the expression
    Name: <input
    type="text"
    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}"/>
    </c:forEach>
    </c:if>
    </spring:bind>
    Brandon Goodin

  2. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default Re: Repopulating Failed form values

    Quote Originally Posted by phasews
    I was curious about how Spring maintains form values to repopulate the html form upon a failed validation.
    </spring:bind>
    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)

    Alef

  3. #3
    Join Date
    Aug 2004
    Posts
    5

    Default

    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.
    Brandon Goodin

  4. #4
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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

    Alef

  5. #5
    Join Date
    Aug 2004
    Location
    Mount Joy, PA
    Posts
    34

    Default

    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.
    ~ Daniel Miller

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. MySQL DDL
    By analogueboy in forum Security
    Replies: 5
    Last Post: Aug 17th, 2007, 03:48 PM
  3. Replies: 4
    Last Post: Jun 13th, 2006, 05:21 PM
  4. Replies: 2
    Last Post: May 5th, 2005, 09:35 PM
  5. Replies: 8
    Last Post: Jan 17th, 2005, 10:14 AM

Posting Permissions

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