简体   繁体   English

h:selectBooleanCheckbox始终将false传递给验证器

[英]h:selectBooleanCheckbox always pass false to validator

I have h:selectBooleanCheckbox but it passes 'false' to the validator. 我有h:selectBooleanCheckbox但是它将'false'传递给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 为什么我有panelGroup是基于页面顶部的逻辑,所以我有逻辑要显示/不显示buttoncheckbox

This is my Validator . 这是我的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 验证程序sysout始终显示checkBoxValue false

UPDATE 更新

After comment on Balusc, I add another sysout to print directly the parameter value . 在对Balusc进行评论之后,我添加了另一个sysout以直接打印参数value But still it's printing as value2: false . 但是它仍然以value2: false的形式打印。

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 有一个与此情况相关的错误,当您渲染一个容器来保存另一个表单中的from时,您所呈现的表单将失去其视图状态,因此,将表单提交给任何目标解决方案都不是从表单中渲染表单容器及其自身另一种形式

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 : 第二个表单提交将如下所示:

在此处输入图片说明

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 : secondForm不会具有其视图状态,因此不会提交表单,但是在服务器上没有目的地,因为在服务器上未获取视图对象,因此需要通过此处执行的操作来获取javax.faces.ViewState值。在f:ajax也渲染表单:

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

That inform server to backup the second form rendered with the javax.faces.ViewState 通知服务器备份使用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? 请参阅该文章: 通过ajax渲染其他表单会导致其视图状态丢失,我该如何重新添加呢? for more info 了解更多信息

Hope that was the problem :) 希望那是问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM