Announcement Announcement Module
Collapse
No announcement yet.
Select becomes empty once validation error occurs Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Select becomes empty once validation error occurs

    I have a form and a select box with list of values in a select box. IF user don't select any value from the drop down list, i will show a validation error on the form.

    The strange thing is: when the form loaded with error message, the drop down (select) box becomes empty.

    Actually i'm getting the values for select boxes from DB.

    Can anybody help me in identifying the problem and how to fix the same. IT will be a great help to me.

    Thanks so much.

  • #2
    It dependens on how you have implemented your code. Please post the jsp page and the controller you're using... and also the spring version could be usefull data.

    Comment


    • #3
      I'm spring 2.5.5

      Actually i'm using 2 controllers one for displaying the page on lick of a hyperlink (which extends AbstractFOrmCOntroller) and then for validating form and displayign successview i'm using another controller which extends SImpleFOrmCOntroller.

      Here is the two controllers used.
      Code:
      public class ShowInviteFriendsController extends AbstractController  {
              
              private RegistrationService regService;
      
              public void setRegService(RegistrationService regService) {
                  this.regService = regService;
              }
      
              
      	public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {
                  System.out.println("ENtering ShowInviteFriendsController -> handleRenderRequestInternal");
                    
                  Registration regObj = (Registration) getPortletContext().getAttribute("regObj");
      
                  SocialGroup[] sgArr = regService.getSocialGroups(regObj);
      
                  System.out.println("Leaving ShowInviteFriendsController -> handleRenderRequestInternal");
                  return new ModelAndView("socialGroup", "sgObjArr", sgArr);            
                  
      	}
      
      }
      The other controller is here.

      Code:
         public class InviteFriendsController extends SimpleFormController {
      
          public InviteFriendsController() { }
          
          private RegistrationService regService;
      
          @Override
          protected void onSubmitAction(ActionRequest request, ActionResponse response, Object command, BindException arg3) throws Exception {
              System.out.println("Entering InviteFriendsController -> onSubmitAction");
              SocialGroup sgObj = (SocialGroup) command;           
              
              Registration regObj = (Registration) getPortletContext().getAttribute("regObj");
              
              System.out.println("values from request object are -----> " + sgObj.getMailIds() );
              
              SocialGroup[] sgArr = regService.getSocialGroups(regObj);
              
              request.setAttribute("sgObjArr", sgArr);
              
              SocialGroup sg = regService.getSocialGroup(sgObj.getSgName(), regObj.getRegEmail());
              sg.setMailIds(sgObj.getMailIds());
              
              regService.sendInvitationToFriends(regObj, sg);
              
              System.out.println("Leaving InviteFriendsController -> onSubmitAction");
              //response.setRenderParameter("action","inviteFriendsInfo");
          }
      
          public void setRegService(RegistrationService regService) {
              this.regService = regService;
          }
          
          
          @Override
          protected void onBindAndValidate(PortletRequest request, Object command, BindException errors) throws Exception {
              //super.onBindAndValidate(arg0, arg1, arg2);
              SocialGroup sgObj = (SocialGroup) command;
              
              validateSgName(sgObj.getSgName(), errors);
              
              String mailIds = sgObj.getMailIds();
              
              if(null == mailIds || "".equalsIgnoreCase(mailIds)) {
                  errors.rejectValue("mailIds", "required.emailaddresses");
              }   
              else {
              boolean flag = false;
              
              String emailStr = "";
              
              StringTokenizer st = new StringTokenizer(mailIds, ",");
              
              while(st.hasMoreTokens()) {
                  emailStr = st.nextToken();
                  flag = validateEmailAddress(emailStr, errors);
                  
                  if(flag) break;
              }
              }
          }
      
          public boolean validateEmailAddress(String emailStr, BindException errors) {
              String input = emailStr;
          
                //Set the email pattern string
                Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
      
                //Match the given string with the pattern
                Matcher m = p.matcher(input);
      
                //check whether match is found 
                boolean matchFound = m.matches();
      
                if(!matchFound) {
                    errors.rejectValue("mailIds", "invalid.emailaddress");
                    return true;
                }
                return false;
          }
          
          private void validateSgName(String sgName, BindException errors) {
              if(( sgName == null) || ("".equalsIgnoreCase(sgName.trim())) || ("-1".equalsIgnoreCase(sgName.trim()))) {
                  errors.rejectValue("sgName", "required.socialGroupName");
              }
          }
      }
      Here is my jsp:

      Code:
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
      <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
      <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
      <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
      <%@ page import="com.mosonex.portal.sdo.SocialGroup" %>
      <% 
      
          SocialGroup[] sgArr = (SocialGroup[]) request.getAttribute("sgObjArr");
      %>
      
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
         "http://www.w3.org/TR/html4/loose.dtd">
      
      <portlet:defineObjects/>
      
      <portlet:actionURL var="aURL">
              <portlet:param name="action" value="inviteFriends"/>
      </portlet:actionURL>
      
      <form:form commandName="sgObj" method="post" action="${aURL}" enctype="application/x-www-form-urlencoded">  
      
      <br>
                   <font color="red"><form:errors path="sgName" /></font>
                   <font color="red"><form:errors path="mailIds" /></font>
      	<br>
                  <br>
      	<table border="0" cellpadding="4">
      		<tr>
                           <td class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="red">*</font> Group Name:</td>
      <!--                 <td><input type="text" class="input" name="sgName" value=""/></td>-->
                           <td>
                               <select class="select" name="sgName" id="sgName"  ><option value="-1">--Not Specified--</option>
                               <% 
                                 if((null != sgArr)  && sgArr.length > 0) {
                                     for(int i=0; i< sgArr.length ; i++) {
                                          //SocialGroup sgObj = sgArr[i];
                                     
                               %>
                               <option value="<%=sgArr[i].getSgName() %>"><%=sgArr[i].getSgName() %></option>
                               <% } } %>
                               </select>
                           </td>
                      </tr>
                      <tr>
                           <td class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="red">*</font> Email Addresses:</td>
                           <td><textarea name="mailIds" value="" rows="8" cols="50"></textarea>
                           </td>
                           </tr>
                      
                      <tr align="center">
                          <td colspan="2"><input type="submit" name="_inviteFriends" value="Invite Friends" onclick="window.location.href='${aURL}';"/></td>                    
                      </tr>
      	</table>
      </form:form>
      Actually i'm unable to populate the social group names drop down box using a simple form controller. SO, i used two controllers.

      PLease let em know what's causing the error. Thank you so much.

      Comment


      • #4
        This problem has resolved by using just one SImpleFormController and overriding referenceData method.

        Hope this helps someone.

        Comment

        Working...
        X