0

Thank you for your help some hours ago! Now, I face a new problem... :(

The field, which should be required if a special radio button is selected cannot change back to "required=false" if the radio button is changed again. The error message, that the field cannot be empty is shown.

Here is my code:

Bean method:

public void required(AjaxBehaviorEvent event) {
    if (test.isRadioButton()) {
        requirement = true;
    } else {
         requirement = false;
    }
}

----

xhtml:

<h:panelGroup id="buttons" layout="block" styleClass="buttonAbstand" >
    <p:selectOneRadio id="radio" value="#{bean.test.radioButton}" required="true" requiredMessage="#{i18n['error.response']}" >
        <f:selectItem itemValue="#{true}" itemLabel="#{i18n['common.ja']}" />
        <f:selectItem itemValue="#{false}" itemLabel="#{i18n['common.nein']}" />
        <f:ajax execute="panel1" listener="#{bean.required}" render="panel1" />
    </p:selectOneRadio>
</h:panelGroup>

<h:panelGroup id="panel1" >
    <h:panelGroup rendered="#{bean.requirement}" >
        <br />
        <h:outputLabel value="#{i18n['input']}" styleClass="labelFont" />      
        <br />
        <p:inputText id="inputText" value="#{bean.test.input}" styleClass="text" required="#{bean.requirement}" requiredMessage="#{i18n['error.input']}" />
    </h:panelGroup>
</h:panelGroup>

My bean has these annotations: @Service @ViewScoped @Getter @Setter.

I would be very glad if you could help me! Best wishes!

  • Just use `requirement = test.isRadioButton()` - you really don't need the `if` in this case. Besides that, your problem might be that you're not updating your form (if this is a form) after calling your function, or you're not setting the `isRadioButton()` properly, can we see the code involving that radiobutton? – Wep0n Jan 12 '18 at 11:37
  • Where and when **should** it be updated? You do not do that anywhere in the code above, so all is normal!!!. Please learn on creating a [mcve] – Kukeltje Jan 12 '18 at 11:40
  • I added the part with the radio buttons to the code-snippet. –  Jan 12 '18 at 11:49
  • Why are you explicitly executing the panel1 during toggling if you actually want to skip its execution? – BalusC Jan 12 '18 at 12:23
  • If the radio button "true" is selected, the panel1 should be rendered an the inputText should be required. Am I missing something? Can I realise this in an other way? –  Jan 12 '18 at 12:40
  • Regarding the execute and render attributes, read https://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes – Kukeltje Jan 12 '18 at 13:03

0 Answers0