[struts]Validator对一个集合(list,数组)进行较验

原帖地址:http://www.coderanch.com/t/55846/Struts/Using-attribute-indexedListProperty-field-tag

 

 

Hi ranchers,
Iam using Validator framework, in one of the form,I have a List which contains Lastname,firstname etc, here I need to to validate these fields..

following is the example code....

<form name="XYZForm">
<field property="lastname" depends="required,mask" indexedListProperty="XYZListObject">

Its not working properly,what are the things to do to make it work properly using indexedListProperty...

Thanks in advance.

----------------------------------------------

Hi Merrill,

Yes, indexedListProperty is supported in Struts and I have used it successfully. If you specify indexedListProperty="XYZListObject", it is expected that the actual property name for the first element will be "XYZListObject[0].firstName", If that is not the case, the validation will not be performed. Be careful with case. Remember that if your getter is getXYZListObject, the property name used by Struts will be "xYZListObject" not "XYZListObject".

can you please give an example code,or correct this to make it work,this is what iam using in validation.xml...

<form name="IPForm">
<field property="lastname" depends="required,mask" indexedListProperty="pListObject">
<arg0 key="lastname" />
<msg name="mask" key="errors.addpax.LastName.required"/>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z ,]*$</var-value>
</var>
</field>
</form>

Thanks in advance,
RR

------------------------------------------------------

I wouldn't be able to tell is your code is correct or not without also seeing your ActionForm bean, the relevant portion of your struts-config.xml file, and the relevant portion of the JSP.

Here is code from a sample page that I have tested and can verify that it works.

struts-config.xml

  1. ...  
  2.        <form-bean name="testRatingForm"   
  3.        type="org.apache.struts.validator.DynaValidatorForm"  >  
  4.        <form-property name="testField"  type= "java.lang.String"  initial= ""  reset= "true" />  
  5.        <form-property name="choices"  type= "java.lang.String[]"  initial= '{"1", "2", "3", "4", "5" }' />  
  6.        <form-property name="criteria"  type= "java.lang.String[]"  reset= "true"  initial= '{"", "", "", "", "", "", "" }' />  
  7.        </form-bean>  
  8. ...  
  9.         <action path="/testRating"   
  10.         type="org.merrill.actions.TestRating"   
  11.         name="testRatingForm"   
  12.         scope="request"   
  13.         validate="true"   
  14.         input="/testRating.jsp" >  
  15.         <forward name="success"  path= "/testRating.jsp" ></forward>  
  16.    
  17.         </action>  

validation.xml
  1.         <form name= "testRatingForm" >  
  2.             <field  
  3.                 property="testField"   
  4.                 depends="required,mask" >  
  5.                     <arg position="0"  key= "test field"  resource= "false" />  
  6.                     <arg position="1"  key= "must by a,z"  resource= "false" />  
  7.                     <var>  
  8.                     <var-name>mask</var-name>  
  9.                     <var-value>^[a-zA-Z]*$</var-value>  
  10.                     </var>  
  11.             </field>  
  12.             <field  
  13.                 indexedListProperty="criteria"   
  14.                 property="criteria"   
  15.                 depends="required" >  
  16.                     <arg key="criteria" />  
  17.             </field>  
  18.         </form>  

testRating.jsp
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type"  content= "text/html; charset=ISO-8859-1" >  
  4. <title>Test rating chart</title>  
  5. </head>  
  6. <body>  
  7. <html:form action="/testRating"  >  
  8. Test Field <html:text property="testField"  />  
  9. <table border="1" >  
  10. <tr>  
  11. <th>Criteria</th>  
  12. <th>poor</th>  
  13. <th>fair</th>  
  14. <th>average</th>  
  15. <th>good</th>  
  16. <th>excellent</th>  
  17. </tr>  
  18. <logic:iterate id="criterion"   name= "testRatingForm"  property= "criteria"  indexId= "x" >  
  19. <tr>  
  20. <td>criterion <%=x %></td>  
  21. <logic:iterate id="choice"  name= "testRatingForm"  property= "choices"  type= "java.lang.String" >  
  22. <td>  
  23. <html:radio property='<%="criteria["+x+"]" %>'  value= "<%=choice %>" />    
  24. </td>  
  25. </logic:iterate>  
  26. </tr>  
  27. </logic:iterate>  
  28. </table>  
  29. <html:errors  />  
  30. <html:submit value="OK"  />  
  31.   
  32. </html:form>  
  33. </body>  
  34. </html> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值