0

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
  • What is the validation error. I can't see you are using validations here. Could you provide the relevant lines from stack trace and the signature of `setSelectedOptionId()` method. – prageeth Nov 22 '12 at 03:58
  • After submitting a form in jsf (which is known as postback), different life cycle events are incorporated, like binding value, validation. and these phase events are executed in any normal flow, before going to invoke application phase, where our application logic codes reside. The thing is radio Button validator rejects value when no radio button is selected and redirects to the form again. – Sazzadur Rahaman Nov 22 '12 at 04:32
  • The described problem would only occur if you have a `required="true"` on the ``, but you have none. The code looks fine so far. It works fine for me. The problem is caused elsewhere. An SSCCE would help a lot. – BalusC Nov 23 '12 at 17:06
  • Thanks @BaluC ! Yes, the problem should occur when `require="true"` but for my case I don't understand why its happening. But I found a work around. – Sazzadur Rahaman Nov 23 '12 at 17:31

1 Answers1

0

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.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52