0

I have h:selectBooleanCheckbox but it passes 'false' to the validator. always.

<h:panelGroup id="tncPanel">
 <label>
  <h:selectBooleanCheckbox id="tncSelection" value="#{businessBean.tncSelection}">
   <f:validator validatorId="checkboxValidator"/>
   <f:attribute name="label" value="Please agree terms and conditions."/>
  </h:selectBooleanCheckbox>
  I have read and agreed to all the
  <a href="#" data-toggle="modal" data-target="#modal-term-and-conditions">
     Terms and Conditions.
  </a>
 </label>
</h:panelGroup>

<h:panelGroup id="buttonPanel">
   <h:commandLink id="nextButton" action="#{businessBean.goNext}">
        Submit
   </h:commandLink>
</h:panelGroup>

Why I have panelGroup here is, based on logic in top of page, I have a logic to display/not the button and checkbox

This is my Validator.

    @FacesValidator("checkboxValidator")
public class CheckboxValidator implements Validator {

    private static final String DEFAULT_LABEL = "Please confirm the address.";

    @Override
    public void validate(FacesContext context, UIComponent component, Object value){
        String checkBoxValue = String.valueOf(((HtmlSelectBooleanCheckbox) component).getSubmittedValue());

        System.out.println("checkBoxValue " + checkBoxValue);
        System.out.println("value2: " + value);   

        String label = String.valueOf(component.getAttributes().get("label"));
        if ("null".equals(label)) {
            label = DEFAULT_LABEL;
        }
        if("false".equalsIgnoreCase(checkBoxValue)){
            FacesMessage msg = new FacesMessage(null, label);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }
}

Validator sysout always prints checkBoxValue false

UPDATE

After comment on Balusc, I add another sysout to print directly the parameter value. But still it's printing as value2: false.

ever alian
  • 1,028
  • 3
  • 15
  • 45
  • @BalusC I am not clear what you are mentioned. I am new to JSF. – ever alian Jan 21 '16 at 09:53
  • @BalusC This is the existing code. Not sure how they did it. but I try like this as well now `HtmlSelectBooleanCheckbox checkbox = (HtmlSelectBooleanCheckbox) component;` Still it's `false` – ever alian Jan 21 '16 at 10:00
  • Your code is perfectly legal. Do you have any listeners registered to your project any class that `implments javax.faces.event.PhaseListener` ? These listeners can run before validation phase and change checkbox value – Youans Jan 21 '16 at 12:10
  • Also check inspect form using Chrome inspector or anything switch to network when form submitted and check request form data you must see something like that `j_idt5:tncSelection:on // true case` to make sure that value is sent to server properly – Youans Jan 21 '16 at 12:13
  • @YouYou I need to mention that this submit button and checkbox contained panel is updated with user input. This is happened as an ajax update. so is there anything relate to this update? after update something happening? – ever alian Jan 21 '16 at 15:20
  • @YouYou Thanks for the valuable tip !!. I tried to see the form data. But i cannot find any form data like `mainForm:tncSelection:on`. Seems it is not passing at all? but If I try to see in`onclick ` event, the value from a java script, I can see the value. `alert(document.getElementById('mainForm:tncSelection').checked);` – ever alian Jan 22 '16 at 02:38
  • Sorry I don't get the updated panel with ajax request part . So it that like a rendered panel from `f:ajax` on another form try provide all details that concern this form submission as obvious now our main problem that value is not submitted – Youans Jan 22 '16 at 20:12
  • @YouYou Yes, there is another include file was there and that had a form. That form has a ajax update for this particular form panel. Will that cause this error? if yes, can you explain me how that happen? how that form can hide this check box? – ever alian Jan 27 '16 at 01:26
  • So the problem is "There no values submitted from form which is rendered from another form" not the tail problem which you sure tried to search for and also the title for that question "There is no value in the validator" . The way you ROOTING the problem the faster you solve ;) – Youans Jan 28 '16 at 12:34
  • Try to put a ID to your form, like this: – David Jun 15 '17 at 17:23

1 Answers1

1

There is a bug related to this situation , When you render a container that holds a from from another form you the rendered form loses its view state ,Thus form is submitted to no destination solution is to render the form container and the form its self from the other form

For example that won't work :

  <h:panelGroup id="panel">
       <h:form>
        <h:commandButton value="Submit" rendered="#{not testAction.flag}">
                <f:ajax render=":panel" listener="#{testAction.submit()}"/>
            </h:commandButton>
        </h:form>
      <h:panelGroup id="panel">

        <h:form id="secondForm" rendered="#{testAction.flag}">
            <h:selectBooleanCheckbox id="checkBox" value="#{testAction.boolVal}">
                <f:validator validatorId="checkboxValidator"/>
            </h:selectBooleanCheckbox>
            <h:commandButton value="Go" action="#{testAction.go()}"/>

        </h:form>
     </h:panelGroup>

Second form submit will be like this :

enter image description here

secondForm won't have its view state the form is submitted but with no destination on the server cuz the view object is not fetched on the server that requires the javax.faces.ViewState value to be fetched by what you have to do here is to render form as well in the f:ajax:

<f:ajax render=":panel :secondForm" listener="#{testAction.submit()}"/>

That inform server to backup the second form rendered with the javax.faces.ViewState

Refer to that post : Rendering other form by ajax causes its view state to be lost, how do I add this back? for more info

Hope that was the problem :)

Community
  • 1
  • 1
Youans
  • 4,801
  • 1
  • 31
  • 57