You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by st...@spamgourmet.com on 2011/07/18 18:55:31 UTC

Form specific error messages when one page has two forms with same field names

Short version:  Is there a way to addFieldError( String fieldname, String message ) specific to a particular form when one page has many forms each with a "fieldname" input?

Long version:
Imagine that you have an editable list of items, but they are each in their own form.  They share the same field name:

<#list books as book>
  <@s.form action="updatebook" id="booktitleform-idx-${book_index?c}">
    <@s.hidden name="book.id" value=book.id />
    <@s.textfield key="book.title" />
    <@s.submit />
    <br /> <hr />
  </...@s.form>
</#list>

UpdateBookAction has:

public void validate() {
  // The action only operates on one book at a time.  It is loaded through a prepare() method or injected already
  if ( book.getTitle() == null || "".equals( book.getTitle().trim() ) {
    addFieldError( "book.title", "Please enter a title for this book entry." );
 }
}

If an error occurs for one book then a red message appears for ALL books, not just for the particular book that is being edited.  Is it possible for the action to be aware of what the form ID is and apply the field error only to the form that submitted?