validationjsfseamjsf-1.2selectoneradio

Accepting the form submission with not selecting any radio button


I am writing an application with Java EE using JSF 1.2 and the Seam framework. I have a form which takes input from radio button like this:

<h:selectOneRadio value="#{testAction.selectedOptionId}"
                 id="selectedQuestionOption"
                 layout="pageDirection">
    <s:selectItems var="selectedOption" value="#{currentQuestion.options}"
                   label="#{selectedOption.optionString}"
                   itemValue="#{selectedOption.optionId}"/>
 </h:selectOneRadio>

 <h:commandButton id="goToNextQuestion" value="Submit"
                 action="#{testAction.postAnswer}"/>

I want that, I would accept the result if anyone submits the form without selecting a radio button, but it does not work. Because in the validation phase jsf rejects the submission and results in a validation error. I tried to write a custom validator for it, unfortunately that also did not worked for me.

Any suggestion?


Solution

  • 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.