• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

validation of indexed properties

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am using indexed properties in my struts application , and all i well except that it doesnt do validation. My form bean is populated with a list of objects and upon submission i want errors listed for each object's invalid values.

Following is the JSP:
/////////////////////////////////////////////////////////////////////
<html:form action="invoice.form?action=updatePO" method="post">
<logic:iterate id="purchaseOrderProduct" name="invoicePOForm" property="purchaseOrderProductList">
<html:hidden name="purchaseOrderProduct" property="productId" indexed="true"/>
<tr>
<td><bean:write name="purchaseOrderProduct" property="productId"/></td>
<td><bean:write name="purchaseOrderProduct" property="productTitle"/></td>
<td><bean:write name="purchaseOrderProduct" property="orderedQty"/></td>
<td><bean:write name="purchaseOrderProduct" property="receivedQty"/></td>
<td><bean:write name="purchaseOrderProduct" property="invoicedQty"/></td>
<td><bean:write name="purchaseOrderProduct" property="costPrice"/></td>
<c:if test="${purchaseOrderProduct.clearedStatus==0}">
<td>£<html:text name="purchaseOrderProduct" property="invoiceUnitPrice" size="30" indexed="true"/></td>
<td>£<html:text name="purchaseOrderProduct" property="invoiceTotalPrice" size="30" indexed="true"/></td>
<td><html:text name="purchaseOrderProduct" property="invoiceQty" size="30" indexed="true"/></td>
<td>Clear<html:multibox property="selectedProductIds"><bean:write name="purchaseOrderProduct" property="productId"></bean:write></html:multibox> </td>
</c:if>
<c:if test="${purchaseOrderProduct.clearedStatus==1}">
<td>£<bean:write name="purchaseOrderProduct" property="invoiceUnitPrice"/> </td>
<td>£<bean:write name="purchaseOrderProduct" property="invoiceTotalPrice"/> </td>
<td><bean:write name="purchaseOrderProduct" property="invoiceQty"/> </td>
<td>Cleared</td>
</c:if>
<td><html:errors property="invalidInvoicePrice"></html:errors></td>
</logic:iterate>
<%-- <td<html:errors property="invalidInvoicePrice"></html:errors></td>-- This bit fails as errors maybe of a number of objects' values.%>
</tr>
////////////////////////////////////////////////

any idea how to generate the errors messages.


Following is the error handling method in the form bean:


public ActionErrors validateForm(ActionMapping actionMapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();
if(getPurchaseOrderProductList().size() >0)
{
for(PurchaseOrderProduct pop:m_aPurchaseOrderProductList) {
if (pop.getInvoiceUnitPrice()<0)
{
StrutsUtils.addActionError(errors,INVALID_INVOICE_PRICE,ERROR_INVALID_INVOICE_PRICE);
}
}
}


return errors;
}
}
 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any advice would be very much appreciated. i have searched the net and hav found nothing useful.
 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me rephrase the problem: can i even validate an indexed property?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can validate indexed properties by using the indexedListProperty attribute of the <field> element in your validation.xml file. You can find examples of this in the Struts Validator Guide.

However, if you do this, I'd recommend you just put a sigle, all-inclusive <html:errors /> tag in your JSP. I don't think there's an easy way to separate the errors by field with indexed properties.
 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
For any one interested the following code performs basic indexed properties validation without the use of validation.xml

/////////////////////////////////////////////////////////////////////
[CODE] public ActionErrors validateForm(ActionMapping actionMapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();


if(request.getParameter("action").equals("updatePO")) {
if(getPurchaseOrderProductList().size() >0)
{
Iterator iter=m_aPurchaseOrderProductList.iterator();
while(iter.hasNext())
{
PurchaseOrderProduct pop=(PurchaseOrderProduct)iter.next();
try {
Double.parseDouble((pop.getInvoiceUnitPrice()));
Integer.parseInteger((pop.getInvoiceQty()));
if(Integer.parseInt(pop.getInvoiceQty())<0) {
StrutsUtils.addActionError(errors,INVALID_INVOICE_PRICE+pop.getProductId(),ERROR_INVALID_INVOICE_PRICE);
}
}
catch (Exception e)
{
StrutsUtils.addActionError(errors,INVALID_INVOICE_PRICE+pop.getProductId(),ERROR_INVALID_INVOICE_PRICE);
}


}

/CODE]


///////////////////////////
<logic:messagesPresent property="invalidInvoicePrice${purchaseOrderProduct.productId}">
<img src="<c:url value="/images/error.gif"/>" alt="<html:errors property="invalidInvoicePrice${purchaseOrderProduct.productId}"/>"/>
</logic:messagesPresent>
////////////////////////////
 
This one time, at bandcamp, I had relations with a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic