java - How to use XML validation for checkboxlist and select in Struts 2 on JSP? -


this jsp file:

<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>company data</title> </head> <body> <center><h3>company data</h3></center> <s:form action="companydata" validate="true">     <s:textfield label="name" key="companyname" />     <s:textfield label="isin" key="companyisin" />     <s:checkboxlist list="exchangeslist" label="exchange" name="exchange" />     <s:select list="sectorslist" label="sector" name="sector" />     <s:checkboxlist list="shareholderslist" label="shareholders" name="shareholders" />     <s:submit /> </s:form> </body> </html> 

here, shareholderslist arraylist<string> , shareholders string.

this action-validation file:

<!doctype validators public  "-//opensymphony group//xwork validator 1.0.2//en" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="companyname">     <field-validator type="required">         <message>you must enter name companyname</message>     </field-validator> </field>  <field name="companyisin">     <field-validator type="required">         <message>you must enter id companyisin</message>     </field-validator> </field>  <field name="exchange">     <field-validator type="fieldexpression">         <param name="expression">             <![cdata[ isdonotcheck() ? true : (exchange != null && !exchange.isempty()) ]]>             <!-- or -->             <!-- isdonotcheck() ? true : !object.field.isempty() -->         </param>         <message>you must check checkboxes exchange</message>     </field-validator> </field>  <field name="shareholders">     <field-validator type="fieldexpression">         <message>you must check checkboxes shareholders</message>     </field-validator> </field>       </validators> 

here screenshot on submitting form:

enter image description here

validations textboxes working fine checkboxlist it's not working @ all. want checkboxlist validation user must select @ least 1 of checkbox list. should done correct code?

also, have similar select list instead of checkboxlist in other jsp, how can list validated too. found many things on internet nothing worked till now.

try following code:

<field name="exchange">     <field-validator type="fieldexpression">         <param name="expression">exchange.length() > 0</param>         <message><![cdata[ must check checkboxes exchange ]]></message>     </field-validator> </field>  <field name="shareholders">     <field-validator type="fieldexpression">         <param name="expression">shareholders.length() > 0</param>         <message><![cdata[ must check checkboxes shareholders ]]></message>     </field-validator> </field>       

if works, start adding custom conditions (isdonotcheck()...).

otherwise, use non-field validator, works sure:

<validator type="fieldexpression">     <param name="fieldname">exchange</param>     <param name="expression"><![cdata[exchange.length() > 0]]></param>     <message><![cdata[ must check checkboxes exchange ]]></message> </validator>  <validator type="fieldexpression">     <param name="fieldname">shareholders</param>     <param name="expression"><![cdata[shareholders.length() > 0]]></param>     <message><![cdata[ must check checkboxes shareholders ]]></message> </validator> 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -