Announcement Announcement Module
No announcement yet.
Simple question about validation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple question about validation

    I have a form that takes some data like name, email address, age etc from the user and submits them into the database. I have a validator that checks to see that the mandatory fields are filled up by the user before inserting the data in the database. So far so good.

    I have requirement that should not allow the user who have already entered their data in the database to enter it again. For that I first get the record from the database for a submitted email address. if the record exist in the database for that email address I need to inform the user about it in a fashion that they are taken back to the same form with all the data that they have entered in a prepopulated mode. This is where I am stuck.

    I am using controller that extends the SimpleFormController. The URL to get the form is http://servernameort/en-US/ So when the email address exist piece of code is
    ModelAndView modelAndView = new ModelAndView("redirect:/en-US/","responseform", userResponse);
    return modelAndView;
    //responseform is the name of the command name
    Following is my controller declaration in the main-servlet.xml:
    <bean id="ttg.responsecontroller" class="com.product.controller.ResponseController">
    <property name="sessionForm" value="true" />
    <property name="commandName" value="responseform" />
    <property name="commandClass" value="com.product.dto.Response" />
    <property name="responseService" ref="ttg.responseservice" />
    <property name="validator">
    <bean class="com.product.validator.ResponseValidator" />
    <property name="formView" value="displayResponseForm" />
    <property name="successView" value="responseThankYou" />
    I am using tiles and my tiles definition is:
    <definition name="displayResponseForm" extends=".mainTemplate" >
    <put-attribute name="body" value="/WEB-INF/jsp/pages/product/responseForm.jsp" />

    <definition name="en-US/responseThankYou" extends=".mainTemplate" >
    <put-attribute name="body" value="/WEB-INF/jsp/pages/product/responseThankYou.jsp" />
    When I redirect using the above code I do get to the form but somewhere my bean "responseform" gets lost and the form is empty. I need the data to be populated.

  • #2
    onBindAndValidate() in the org.springframework.web.servlet.mvc.AbstractFormCo ntroller

    onBindAndValidate() in the org.springframework.web.servlet.mvc.AbstractFormCo ntroller is the solution to my problem. After reading the documentation for this class I finally got it. The onBindAndValidate() method is called post validating all the fields in the form. Since this method is overridden in the controller classes you can do the database validation over here. This is a very clean approach rather than having the database insertion code inside the validator and then validating your data.


    • #3
      showForm() method also serves the same purpose

      You can also use showForm() method if you want to return to the form with all the data pre-populated in case if the database validation failed.