I did not find any solution to this problem by writing a custom validator which should be the ideal solution in this case. But I found a work around to overcome this situation. I can bypass the binding value and validation phases by setting immediate = "true"
of the submit button like this:
<h:commandButton id="goToNextQuestion" value="Submit" immediate="true"
action="#{testAction.postAnswer}"/>
By doing this skipping the subsequent phases faces servlet will go to invoke application phase and your application logic will be applied and as you skipped the processing phases the data from your post request will not be bind to your beans. So before working with those beans you should read their value from the RequestParameterMap
like I did bellow(in my case):
Map<String, String> paramMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
for (String key : paramMap.keySet()) {
//log.info("Param: " + key + " Value: " + paramMap.get(key));
if (key.contains("selectedQuestionOption")) {
//log.info("OptionParam: " + key + " Value: " + paramMap.get(key));
selectedOptionId = Integer.parseInt(paramMap.get(key));
}
}
and then applied my real business logic.