ponder275 ponder275 - 7 months ago 62
HTML Question

How to highlight fields which fail Struts2 validation?

I have a user who would like to have the fields which fail Struts2 validation highlighted.

I plan to set a flag for any field which fails in my

validate()
method and use that field to change the background of the field when my JSP page is loaded.

My question is how do I handle the fields which fail the validation in my
Action-validation.xml
file ? Do I need to do all of my validation in my
validate()
method ?

Also is there a way to highlight the fields which generate an


Invalid field value for field ...


error ?

Any help would be appreciated!

Answer

This feature is already built in Struts2, no need for custom flags or whatever. In Struts2 form tags there is cssErrorClass attribute which will set CSS class for field on error.

<style type="text/css">
    .errField {
        background-color: red;
    }
</style>

<s:textfield key="name" cssErrorClass="errField" />