1

I read through this Struts2 Validation for an array

and it makes sense, but wished person (Quaternion) would explain how to

"rewrite the above to specifically name fields (with indexes) in which case you can use , and you'll use the addFieldError method. For details on these tags see http://struts.apache.org/2.3.1.2/docs/tag-reference.html"

This is what I have:

<s:form action="saveOrUpdateAction" method="get">
    <s:token/>
    <table>
        <tr>
            <td> Fund </td>
            <td> Award Code </td>
        </tr>
        <s:iterator value="gfeListWithEmptyCode">
            <tr>
                <td> <s:property value="sfafund "/> </td>
                <td> <s:property value="awardcode"/>
                    <input type="text" name="codeArray">
                </td>
            </tr>
        </s:iterator>
        <s:token />
        <s:submit key="Submit2"/>
    </table>
</s:form>

Part of my Action:

public void validate()
{
    if (fund == null || fund.trim().length() != 5 )
    {
        System.out.println("testing+++++++++++++++++++1");
        addFieldError("fund","Fund requires 5 characters.");
    }
    if (code == null || code.trim().length() != 3 )
    {
        System.out.println("testing+++++++++++++++++++2");
        addFieldError("code","Fund requires 3 characters.");
    }


    if (gfeListWithEmptyCode !=null)
    {

        int index = 0;
        for (GiftFundEntity giftFundEntity : gfeListWithEmptyCode)
        {
            if ( codeArray[index]!=null && codeArray[index].length() < 3 )
            {

                System.out.println("testing+++++++++++++++++++3");

                // technically, this is not possible to do because it requires codeArray[index] and not a string.

                addFieldError("codeArray","Code requires 3 characters.");
                index++;
            }
        }
    }


    try
    {
        this.execute();
    } catch (Exception e)
    {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}

During validation, the red error message does not show up on the jsp page for obvious reasons because codeArray isn't listed by the indexes. How do I get this to work? * Please note* the array is dynamic.

I looked through the struts documentation and searched through stackoverflow, but I don't see how it can be done.

Thanks for your time.

Community
  • 1
  • 1
user3769040
  • 235
  • 4
  • 14

1 Answers1

0

The answer is Struts 2 treats the codeArray variable inside the Action class as an Array: String [] codeArray; This is not documented.

user3769040
  • 235
  • 4
  • 14