Hello,
I was need to manage focus on the first field with error. With your
solution, the problem is
errors collection is sorted, so your focus will not go to the first field
with error in your JSP.
So I extends doErrorExist of TextTag to manage it, like this :

protected boolean doErrorsExist() throws JspException {
       boolean errorExist = super.doErrorsExist();
       ServletRequest request = pageContext.getRequest();
       if (errorExist && request.getAttribute("FIRST_FIELD_WITH_ERROR" ) ==
null) {
           request.setAttribute("FIRST_FIELD_WITH_ERROR" ,
super.getPropertyExpr());
       }
       return errorExist;
}

So if field has error, request will be updated.
After at end of your JSP, you write call function Javascript, like this

<c:if test="${requestScope.FIRST_FIELD_WITH_ERROR != null}" >
setFocusToField('<c:out value="${requestScope.FIRST_FIELD_WITH_ERROR}" />');
<c:if>

Here jaavscript function :

function setFocusToField(fieldName, fieldIndex) {
   if (fieldIndex == null || fieldIndex.length < 1)
       fieldIndex = 0;
   var listElementHTML = document.getElementsByName(fieldName);
   if (listElementHTML != null &&
       listElementHTML.length > 0) {
        var elementHTML = listElementHTML[fieldIndex];
       if (elementHTML.disabled == false) {
            /*if (elementHTML.type == "text")
                elementHTML.select();
            else */
                elementHTML.focus();
       }
    }
}


Regards Angelo




2006/9/12, Strachan, Paul <[EMAIL PROTECTED]>:


maybe we could do something like this in the JSP - the idea is taken
from the TextTag.java which knows to use the errorStyleClass when theres
an error.

...
<%@ page import="java.util.Iterator" %>
<%@ page import="org.apache.struts.Globals" %>
<%@ page import="org.apache.struts.taglib.TagUtils" %>
<%@ page import="org.apache.struts.action.ActionMessages" %>

<%
String errorKey = Globals.ERROR_KEY;
ActionMessages errors = TagUtils.getInstance()
                            .getActionMessages(pageContext, errorKey);

Iterator iter = errors.properties();
String focusProperty = "some_default_property";

if (iter.hasNext()) {
  focusProperty = (String) iter.next(); // default to first property
} %>

<html:form focus="<%= focusProperty %>" action="/someAction.do"
method="post"> ...
</html:form>


Notes:

1. The default_property is the field you would normally focus on if
there is no error.

2. It would be best to put this code in a tag library.

3. The javascript generated by the struts "focus" attribute may need to
be improved (e.g. in a try...catch) just in case a property doesnt
exist.


Please let me know how this approach looks as I am also considering this
for my app.

Thanks,

Paul
**********************************************************************
This message is intended for the addressee named and may contain
privileged information or confidential information or both. If you
are not the intended recipient please delete it and notify the sender.
**********************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to