jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes validation of indexed properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validation of indexed properties" Watch "validation of indexed properties" New topic
Author

validation of indexed properties

ahsan mir
Ranch Hand

Joined: Jul 17, 2006
Posts: 42
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

Joined: Jul 17, 2006
Posts: 42
any advice would be very much appreciated. i have searched the net and hav found nothing useful.
ahsan mir
Ranch Hand

Joined: Jul 17, 2006
Posts: 42
let me rephrase the problem: can i even validate an indexed property?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
ahsan mir
Ranch Hand

Joined: Jul 17, 2006
Posts: 42
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>
////////////////////////////
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validation of indexed properties
 
Similar Threads
Javascript - Arraylist
Displaying Validation Errors with Indexed Properties
Indexed Properties Example
Struts: No getter method for property name problem
html:text inside logic:iterate