Here is my validate method –
@Override
public void validate() {
errors = new HashMap<String, String>();
if(StringUtil.isBlank(examCode)){
errors.put("examCode", "Exam code is required");
}
if(StringUtil.isBlank(strPaperType)){
errors.put("paperType", "Paper Type is required");
}else{
paperType = PaperType.getPaperTypeByValue(strPaperType);
if(paperType == null){
errors.put("paperType", "A valid Paper Type is required");
}
if(paperType.equals(PaperType.PRACTICE)){
if(topicId ==null){
errors.put("topicId", "Topic Id is required");
}
}
}
if(StringUtil.isBlank(instructions)){
errors.put("instructions", "Paper Instructions are required");
}
}
‘errors’ is my own map defined in the action. I’m not adding any errors to ‘fieldErrors’. What’s happening is even before entering my ‘validate’ method if I debug ‘fieldErrors’ I see following two entries –
{"id":["Invalid field value for field \"id\"."],"topicId":["Invalid field value for field \"topicId\"."]}
I have no idea from where are they getting added. Here is my struts conf.
<package name="api" extends="json-default" namespace="/api">
<action name="paper" class="paperApiAction">
<result name="json" type="json">
<param name="root">responseDto</param>
</result>
<result name="input" type="json">
<param name="root">fieldErrors</param>
</result>
</action>
</package>
Need help with this. Thanks
The “conversionError” interceptor will take type conversion errors and put them into
fieldErrors. There are usecases where it’s easier to take it out of the stack; I’m not sure if this is one of them or not.Why bother duplicating the
fieldErrorsmap? Even if you just want to have a map for use in your own DTO, why not use the existing validation mechanisms? The difference is minuscule, and a bit more flexible. You could then build the paper type validation into externalized business logic and simplify testing both it, and the action.Unrelated, but I find your code difficult to read because of the lack of whitespace. A naive refactoring: