You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Burton Rhodes <bu...@gmail.com> on 2011/03/25 03:59:55 UTC

[S2] Collections Validation (For Dynamic Form Fields)

I am trying to think through creating a page that allows the users to
dynamically add "phone" form fields for a "contact" (Contact model has
a collection on Phone objects).   The method is similiar to this
example: http://www.quackfuzed.com/demos/jQuery/dynamicField/generalUse.cfm

Where I am having trouble is how to implement validation on the
collection of Phone objects and how to use addFieldError() in my
Validation function so that "cssErrorClass" works correctly.  I have
googled around but have found bits and pieces with no clear answer.
How does Struts 2 handle validating elements in a collection?

I have written a bit of pseudo-code that might explain a bit more.
Has anyone done something like this?  Am I missing a simple solution?

::Model::
public class Contact {
  private Integer contactId;
  private Collection<Phone> phones;
}

::Action::
public class ContactCreateAction extends ActionSupport implements Validateable {

  public void validateCreate() {

    // Validate Phone Entries Here
    for (Phone phone : contact.getPhones()) {
       if (phone.getNumber().length < 10) {
           addFieldError(??HOW??);
       }
    }

  }
}

::JSP::
<!-- Display Phone Numbers -->
<s:iterator var="phone" value="%{contact.phones}" status="rowstatus">
    <s:textfield name="contact.phones" value="%{#phone.number}"
cssErrorClass="ErrorDataField" />
</s:iterator>
<!-- This section will also contain Javacript to add/remove phone
fields on the page -->

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org