3

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!

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
ponder275
  • 903
  • 2
  • 12
  • 33

2 Answers2

5

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" />
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • This worked great when I put the ` – ponder275 May 02 '16 at 21:10
  • 2
    Not at all: you are either not loading the right CSS, having it cached, having typos or getting into [specificity](http://stackoverflow.com/a/21728972/1654265) problems. I bet on cache. – Andrea Ligios May 02 '16 at 22:31
  • I put the css in the same file which contains all of the rest of the css for the project so I think it is in the right place. I tried the page with Firefox and it worked but it still doesn't work with IE so I think that supports your bet on cache. I have restarted my computer and it still doesn't work. How else do I clear the cache? Thanks! – ponder275 May 03 '16 at 13:10
  • 2
    It was the cache. I am running IE 11 and used F12->Network->Clear Cache (third Icon from the left). Thanks again! – ponder275 May 03 '16 at 13:31
2

Do I need to do all of my validation in my validate() method ?

No

How do I handle the fields which fail the validation in my Action-validation.xml file ?

Simply drop the flag idea, you're reinventing the wheel.

When a validation fails, no matter if it through annotation or XML, a fieldError is added. When instead you use the validate() method, you should add it by yourself.

The result, however, is the same: you'll have a fieldError with the name of every field not passing one or more validations.

Note that:

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

Invalid field value for field ...

error ?

those are conversion errors. They're added by the Conversion Interceptor (read more).

In the end, no matter if XML, validate(), conversion or whatever, every kind of fielderror will be in that object, that is a

Map<String,List<String>>

because every field could have multiple errors.

You can iterate that map in the page to highlight the related fields through Javascript (or even CSS, when using the simple theme, because with the other themes is just a mess).

For example:

<style>
    .highlightedError {
        border: 2px solid red;
    }
</style>

<script>
    $(function(){
        <s:if test="hasFieldErrors()">
            <s:iterator value="fieldErrors">
                var currentFieldError = '<s:property value="key" escapeJavascript="true" />';
                $('[name="'+currentFieldError+"']").addClass('highlightedError');
            </s:iterator>
        </s:if>
    });
</script>
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243