You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Carl Smith <cc...@yahoo.com> on 2006/03/24 23:29:48 UTC

indexed property validation: how to keep the value the user entered

I have a jsp containing an indexed test box field, and I need to validate the user enter a value into all the text boxes when clicking on the save button. Validation is done OK, but there is an issue. Let me give you an example illustrating the issue: if there are three text boxes, the user enters 1, 2 into the first two boxes, but didn't enter anything in the third box, struts validation catch and display the correct validation error on the jsp saying he/she needs to enter values for all the text boxes, then it wipes out 1 and 2, which is not what I wanted. What I wanted was that when displaying the validation error, it should keep 1 and 2 which was entered by the user. Any suggestions? I appreciate your helps!
   
  Here are my classes:
   
  public class TestingForm extends ValidatorActionForm {
  private LabelValueBean[] listOfItems ;
  public LabelValueBean[] getListOfItems() {
  return listOfItems;
  }
  public void setListOfItems(LabelValueBean[] beans) {
  listOfItems = beans;
  }
  }
  public class DisplayAction extends org.apache.struts.action.Action { 
  
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws NestedException {
  
  TestingForm testingForm = (TestingForm) form;
  LabelValueBean[] listOfItems = new LabelValueBean[3];
  
  LabelValueBean bean1 = new LabelValueBean("1", "");
  LabelValueBean bean2 = new LabelValueBean("2", "");
  LabelValueBean bean3 = new LabelValueBean("3", "");
  listOfItems[0]=bean1;
  listOfItems[1]=bean2;
  listOfItems[2]=bean3;
  
  testingForm.setListOfItems(listOfItems); 
  
  return mapping.findForward("myJsp.jsp");
  }
  }
  myJsp.jsp
  <html:form action="/testingSaveAction" method="post">
  logic:iterate name="testingForm" property="listOfItems" id="labelValue">
  Indexed field to be validated: <html:text name="labelValue" property="value" indexed="true"/><br />
  /logic:iterate> 
  <html:submit property="submit">Save</html:submit>
  </html:form>
   
  In validaton.xml I set up the validation for the indexed filed:
<formset>
 <form name="/testingSaveAction"> 
      <field property="value" indexedListProperty="listOfItems" depends="required">
             <msg name="required" key="thisfield.cannotblank"/>
       </field>
          
 </form>
</formset>

		
---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.

Re: indexed property validation: how to keep the value the user entered

Posted by Carl Smith <cc...@yahoo.com>.
Your comments make great sense...
   
  I moved the initializing part from the displayAction to the reset method in the form bean as you suggested and it works as expected. 
   
  Thanks.
  
Laurie Harper <la...@holoweb.net> wrote:
  OK, so when validation fails Struts forwards to displayTesting which, in 
turn, forwards to /displayAction. In your DisplayAction class, you're 
overwriting the values of your form bean property, destroying the 
information Struts set there.

You'll need to check if the property has already been initialized and 
not overwrite its values if it has any. Alternatively, you could move 
the code for initializing the property in the form bean's reset() 
method, but probably leaving it where it is and wrapping it in a guard 
would be a better bet.

Alternatively, split your DisplayAction into two actions, one to do the 
initial setup and one to handle re-display of the form. In either case, 
to goal is to not trample the data in the form during re-display.

L.

Carl Smith wrote:
> First of all Thank you Laurie, you are the only one anwering my questions :):
> 
> Here are my struts-config.xml entries for the actions and formbean ( I neglected the path and anything).
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> > name="testingForm" 
> scope="session" 
> validate="false" >
> 
> 
> > name="testingForm" scope="session" 
> validate="true" input="displayTesting">
> 
> 
> 
> 
> 
> Laurie Harper wrote:
> We'll need to see the relevant parts of your struts-config.xml too 
> (specifically, the form bean and action mapping definitions).
> 
> L.
> 
> Carl Smith wrote:
>> I have a jsp containing an indexed test box field, and I need to validate the user enter a value into all the text boxes when clicking on the save button. Validation is done OK, but there is an issue. Let me give you an example illustrating the issue: if there are three text boxes, the user enters 1, 2 into the first two boxes, but didn't enter anything in the third box, struts validation catch and display the correct validation error on the jsp saying he/she needs to enter values for all the text boxes, then it wipes out 1 and 2, which is not what I wanted. What I wanted was that when displaying the validation error, it should keep 1 and 2 which was entered by the user. Any suggestions? I appreciate your helps!
>>
>> Here are my classes:
>>
>> public class TestingForm extends ValidatorActionForm {
>> private LabelValueBean[] listOfItems ;
>> public LabelValueBean[] getListOfItems() {
>> return listOfItems;
>> }
>> public void setListOfItems(LabelValueBean[] beans) {
>> listOfItems = beans;
>> }
>> }
>> public class DisplayAction extends org.apache.struts.action.Action { 
>>
>> public ActionForward execute(
>> ActionMapping mapping,
>> ActionForm form,
>> HttpServletRequest request,
>> HttpServletResponse response)
>> throws NestedException {
>>
>> TestingForm testingForm = (TestingForm) form;
>> LabelValueBean[] listOfItems = new LabelValueBean[3];
>>
>> LabelValueBean bean1 = new LabelValueBean("1", "");
>> LabelValueBean bean2 = new LabelValueBean("2", "");
>> LabelValueBean bean3 = new LabelValueBean("3", "");
>> listOfItems[0]=bean1;
>> listOfItems[1]=bean2;
>> listOfItems[2]=bean3;
>>
>> testingForm.setListOfItems(listOfItems); 
>>
>> return mapping.findForward("myJsp.jsp");
>> }
>> }
>> myJsp.jsp
>>
>> logic:iterate name="testingForm" property="listOfItems" id="labelValue">
>> Indexed field to be validated: 
> 
>> /logic:iterate> 
>> Save
>>
>>
>> In validaton.xml I set up the validation for the indexed filed:
>>
>> 
>>
>>
>>
>>
>>
>>
>>
>>
>> ---------------------------------
>> Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2?min or less.
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 
> 
> ---------------------------------
> Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



		
---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.

Re: indexed property validation: how to keep the value the user entered

Posted by Laurie Harper <la...@holoweb.net>.
OK, so when validation fails Struts forwards to displayTesting which, in 
turn, forwards to /displayAction. In your DisplayAction class, you're 
overwriting the values of your form bean property, destroying the 
information Struts set there.

You'll need to check if the property has already been initialized and 
not overwrite its values if it has any. Alternatively, you could move 
the code for initializing the property in the form bean's reset() 
method, but probably leaving it where it is and wrapping it in a guard 
would be a better bet.

Alternatively, split your DisplayAction into two actions, one to do the 
initial setup and one to handle re-display of the form. In either case, 
to goal is to not trample the data in the form during re-display.

L.

Carl Smith wrote:
> First of all Thank you Laurie, you are the only one anwering my questions :):
>    
>   Here are my struts-config.xml entries for the actions and formbean ( I neglected the path and anything).
>    
>   <form-beans>
>  <form-bean name="testingForm" type="TestingForm" /> 
> </form-beans>
>   
> <global-forwards>
>  <forward name="displayTesting" path="/displayAction.do"/>
> </global-forwards>
>  
>   <action-mappings>
>  <action path="/displayAction" type="DisplayAction" 
>   name="testingForm" 
>   scope="session" 
>   validate="false" >
>   </action>
>   
>  <action path="/testingSaveAction"   type="TestingSaveAction" 
>   name="testingForm" scope="session" 
>   validate="true" input="displayTesting">
>     <forward name="success" path="susscess.jsp" />  
>   </action>
> </action-mappings> 
>   
> 
> Laurie Harper <la...@holoweb.net> wrote:
>   We'll need to see the relevant parts of your struts-config.xml too 
> (specifically, the form bean and action mapping definitions).
> 
> L.
> 
> Carl Smith wrote:
>> I have a jsp containing an indexed test box field, and I need to validate the user enter a value into all the text boxes when clicking on the save button. Validation is done OK, but there is an issue. Let me give you an example illustrating the issue: if there are three text boxes, the user enters 1, 2 into the first two boxes, but didn't enter anything in the third box, struts validation catch and display the correct validation error on the jsp saying he/she needs to enter values for all the text boxes, then it wipes out 1 and 2, which is not what I wanted. What I wanted was that when displaying the validation error, it should keep 1 and 2 which was entered by the user. Any suggestions? I appreciate your helps!
>>
>> Here are my classes:
>>
>> public class TestingForm extends ValidatorActionForm {
>> private LabelValueBean[] listOfItems ;
>> public LabelValueBean[] getListOfItems() {
>> return listOfItems;
>> }
>> public void setListOfItems(LabelValueBean[] beans) {
>> listOfItems = beans;
>> }
>> }
>> public class DisplayAction extends org.apache.struts.action.Action { 
>>
>> public ActionForward execute(
>> ActionMapping mapping,
>> ActionForm form,
>> HttpServletRequest request,
>> HttpServletResponse response)
>> throws NestedException {
>>
>> TestingForm testingForm = (TestingForm) form;
>> LabelValueBean[] listOfItems = new LabelValueBean[3];
>>
>> LabelValueBean bean1 = new LabelValueBean("1", "");
>> LabelValueBean bean2 = new LabelValueBean("2", "");
>> LabelValueBean bean3 = new LabelValueBean("3", "");
>> listOfItems[0]=bean1;
>> listOfItems[1]=bean2;
>> listOfItems[2]=bean3;
>>
>> testingForm.setListOfItems(listOfItems); 
>>
>> return mapping.findForward("myJsp.jsp");
>> }
>> }
>> myJsp.jsp
>>
>> logic:iterate name="testingForm" property="listOfItems" id="labelValue">
>> Indexed field to be validated: 
> 
>> /logic:iterate> 
>> Save
>>
>>
>> In validaton.xml I set up the validation for the indexed filed:
>>
>>   
>>
>>
>>
>>
>>
>>
>>
>>
>> ---------------------------------
>> Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2?min or less.
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 
> 			
> ---------------------------------
> Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: indexed property validation: how to keep the value the user entered

Posted by Carl Smith <cc...@yahoo.com>.
First of all Thank you Laurie, you are the only one anwering my questions :):
   
  Here are my struts-config.xml entries for the actions and formbean ( I neglected the path and anything).
   
  <form-beans>
 <form-bean name="testingForm" type="TestingForm" /> 
</form-beans>
  
<global-forwards>
 <forward name="displayTesting" path="/displayAction.do"/>
</global-forwards>
 
  <action-mappings>
 <action path="/displayAction" type="DisplayAction" 
  name="testingForm" 
  scope="session" 
  validate="false" >
  </action>
  
 <action path="/testingSaveAction"   type="TestingSaveAction" 
  name="testingForm" scope="session" 
  validate="true" input="displayTesting">
    <forward name="success" path="susscess.jsp" />  
  </action>
</action-mappings> 
  

Laurie Harper <la...@holoweb.net> wrote:
  We'll need to see the relevant parts of your struts-config.xml too 
(specifically, the form bean and action mapping definitions).

L.

Carl Smith wrote:
> I have a jsp containing an indexed test box field, and I need to validate the user enter a value into all the text boxes when clicking on the save button. Validation is done OK, but there is an issue. Let me give you an example illustrating the issue: if there are three text boxes, the user enters 1, 2 into the first two boxes, but didn't enter anything in the third box, struts validation catch and display the correct validation error on the jsp saying he/she needs to enter values for all the text boxes, then it wipes out 1 and 2, which is not what I wanted. What I wanted was that when displaying the validation error, it should keep 1 and 2 which was entered by the user. Any suggestions? I appreciate your helps!
> 
> Here are my classes:
> 
> public class TestingForm extends ValidatorActionForm {
> private LabelValueBean[] listOfItems ;
> public LabelValueBean[] getListOfItems() {
> return listOfItems;
> }
> public void setListOfItems(LabelValueBean[] beans) {
> listOfItems = beans;
> }
> }
> public class DisplayAction extends org.apache.struts.action.Action { 
> 
> public ActionForward execute(
> ActionMapping mapping,
> ActionForm form,
> HttpServletRequest request,
> HttpServletResponse response)
> throws NestedException {
> 
> TestingForm testingForm = (TestingForm) form;
> LabelValueBean[] listOfItems = new LabelValueBean[3];
> 
> LabelValueBean bean1 = new LabelValueBean("1", "");
> LabelValueBean bean2 = new LabelValueBean("2", "");
> LabelValueBean bean3 = new LabelValueBean("3", "");
> listOfItems[0]=bean1;
> listOfItems[1]=bean2;
> listOfItems[2]=bean3;
> 
> testingForm.setListOfItems(listOfItems); 
> 
> return mapping.findForward("myJsp.jsp");
> }
> }
> myJsp.jsp
> 
> logic:iterate name="testingForm" property="listOfItems" id="labelValue">
> Indexed field to be validated: 

> /logic:iterate> 
> Save
> 
> 
> In validaton.xml I set up the validation for the indexed filed:
> 
>   
> 
> 
> 
> 
> 
> 
> 
> 
> ---------------------------------
> Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2?min or less.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



			
---------------------------------
Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates.

Re: indexed property validation: how to keep the value the user entered

Posted by Laurie Harper <la...@holoweb.net>.
We'll need to see the relevant parts of your struts-config.xml too 
(specifically, the form bean and action mapping definitions).

L.

Carl Smith wrote:
> I have a jsp containing an indexed test box field, and I need to validate the user enter a value into all the text boxes when clicking on the save button. Validation is done OK, but there is an issue. Let me give you an example illustrating the issue: if there are three text boxes, the user enters 1, 2 into the first two boxes, but didn't enter anything in the third box, struts validation catch and display the correct validation error on the jsp saying he/she needs to enter values for all the text boxes, then it wipes out 1 and 2, which is not what I wanted. What I wanted was that when displaying the validation error, it should keep 1 and 2 which was entered by the user. Any suggestions? I appreciate your helps!
>    
>   Here are my classes:
>    
>   public class TestingForm extends ValidatorActionForm {
>   private LabelValueBean[] listOfItems ;
>   public LabelValueBean[] getListOfItems() {
>   return listOfItems;
>   }
>   public void setListOfItems(LabelValueBean[] beans) {
>   listOfItems = beans;
>   }
>   }
>   public class DisplayAction extends org.apache.struts.action.Action { 
>   
>   public ActionForward execute(
>   ActionMapping mapping,
>   ActionForm form,
>   HttpServletRequest request,
>   HttpServletResponse response)
>   throws NestedException {
>   
>   TestingForm testingForm = (TestingForm) form;
>   LabelValueBean[] listOfItems = new LabelValueBean[3];
>   
>   LabelValueBean bean1 = new LabelValueBean("1", "");
>   LabelValueBean bean2 = new LabelValueBean("2", "");
>   LabelValueBean bean3 = new LabelValueBean("3", "");
>   listOfItems[0]=bean1;
>   listOfItems[1]=bean2;
>   listOfItems[2]=bean3;
>   
>   testingForm.setListOfItems(listOfItems); 
>   
>   return mapping.findForward("myJsp.jsp");
>   }
>   }
>   myJsp.jsp
>   <html:form action="/testingSaveAction" method="post">
>   logic:iterate name="testingForm" property="listOfItems" id="labelValue">
>   Indexed field to be validated: <html:text name="labelValue" property="value" indexed="true"/><br />
>   /logic:iterate> 
>   <html:submit property="submit">Save</html:submit>
>   </html:form>
>    
>   In validaton.xml I set up the validation for the indexed filed:
> <formset>
>  <form name="/testingSaveAction"> 
>       <field property="value" indexedListProperty="listOfItems" depends="required">
>              <msg name="required" key="thisfield.cannotblank"/>
>        </field>
>           
>  </form>
> </formset>
> 
> 		
> ---------------------------------
> Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org