With FormView it's possible to manage error after validation of your form. So you could :
To manage error, you must :
<!-- HTML input type="text" --> <displayer name="input_text" > ... <behaviour name="ERROR" > <insertAttribute name="class" > <![CDATA[error]]> </insertAttribute> </behaviour> </displayer>
This configuration will add class="error" to the HTML input type="text" which has error.
// Save form view WEBFormViewUtil.saveFormView(request, context, mapping.getPath()); // merge errors with struts action errors WEBFormViewUtil.mergeFormViewWithStrutsActionErrors(request, context);
Formview save into request, the first field name which has error with the key firstFieldNameWithError. So you can use this information to set the focus to this field. Formview distribution give a function javascript setFocusToField into the file formview.js, which set the focus to the field name and index :
/** * Fonction setFocusToField permet de positionner * le focus sur le champs HTML de nom <fieldName> * et d'index <fieldIndex> (0 par defaut) */ function setFocusToField(fieldName, fieldIndex) { if (fieldIndex == null) fieldIndex = 0; var listElementHTML = document.getElementsByName(fieldName); if (listElementHTML != null && listElementHTML.length > 0) { var elementHTML = listElementHTML[fieldIndex]; if (elementHTML.disabled == false) { try { if (elementHTML.type == "text") elementHTML.select(); else elementHTML.focus(); } catch(e) {}; } } } }
To set the focus, you must copy this file into js directory of your WEB Application [YOUR_WEBAPP] and include it in your page, like this :
<script type="text/javascript" src="[YOUR_WEBAPP]/js/formview.js></script>
If you have EL capability, you can generate javascript which set the focus like this :
function loadFormViewUseCase() { <c:if test="${firstFieldNameWithError != null && firstFieldNameWithError != ''}" > // Set the focus to th efirst field with error setFocusToField('<c:out value="${firstFieldNameWithError}" />','<c:out value="${firstFieldIndexWithError}" />'); </c:if>
At end you can call this function on event onload on your HTML body, like this :
.... <body onload="javascript:loadFormViewUseCase();" > ....
If you extends to the FormViewDispatchAction, by default action errors is merged with form view fields. But you can, disable this behaviour by calling setMergeFormViewWithStrutsActionErrors in your Action Struts, like this :
WEBFormViewUtil.setMergeFormViewWithStrutsActionErrors(ServletRequest request, false);