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>