仅在所有其他输入均有效时验证h:selectBooleanCheckbox

问题描述 投票:0回答:1

我在JSF中有一个注册表单,只有在表单中的所有其他字段都有效之后,我才想验证新用户是否同意条款和条件。有什么方法可以在JSF中对此进行验证吗?这是我的代码示例。

    <h:form id="registerForm">
        <table>

            //Other fields
            <tr>
                <td><h:outputText value="Username " /></td>
                <td><h:inputText id="fusername"
                        value="#{registerUser.username}">
                        <f:validator for="fusername" validatorId="usernameValidator" />
                    </h:inputText></td>
                <td><h:message for="fusername" style="color:red" /></td>
            </tr>

            <tr>
                <td><h:outputText value="Password: " /></td>
                <td><h:inputSecret id="fpassword"
                        value="#{registerUser.password}">
                        <f:validator for="fpassword" validatorId="passwordValidator" />
                    </h:inputSecret></td>
                <td><h:message for="fpassword" style="color:red" /></td>
            </tr>

            <tr>
                <td><h:outputText value="Repeat Password " /></td>
                <td><h:inputSecret id="repeatPassword"
                        value="#{registerUser.repeatPassword}">
                            <f:validator for="repeatPassword" validatorId="repeatPasswordValidator"/>
                            <f:attribute name="originalPassword" value="fpassword" /> 
                        </h:inputSecret></td>
                <td><h:message for="repeatPassword" style="color:red" /></td>
            </tr>
            <tr>
                <td><h:selectBooleanCheckbox
                        value="#{registerUser.agreeTermsAndConditions}" required="true"/></td>
                <td>I agree with the terms and condition.</td>
            </tr>


            <tr>
                <td />
                <td><h:commandButton action="#{registerUser.saveUser}"
                        value="Register" /></td>
            </tr>

        </table>
    </h:form>


</f:view>

jsf
1个回答
1
投票

您可以在保存方法中的复选框上手动进行验证,因为您已经通过了验证。请注意,您要在复选框中设置一个ID才能找到它。

public void saveUser(){
  if (!agreeTermsAndConditions) {
    FacesContext context = FacesContext.getCurrentInstance();
    // Tell JSF validation has failed
    context.validationFailed();
    // Get the component
    UIComponent component = context.getViewRoot().findComponent("registerForm:agreeTermsAndConditions");
    // Set it to invalid
    ((EditableValueHolder)component).setValid(false);
    // Create a message
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You must agree with the terms and conditions.", null);
    // Register the message for the component
    context.addMessage(component.getClientId(), message);
  }
  // Agreed, so save the user here
  ... 
}

并且在您的视图中,添加用于查找组件的ID和h:message以显示设置的消息。

<h:form id="registerForm">
  <table>
    ...
    <tr>
      <td><h:selectBooleanCheckbox id="agreeTermsAndConditions"
                                   value="#{registerUser.agreeTermsAndConditions}"/></td>
      <td>I agree with the terms and condition.</td>
      <td><h:message for="agreeTermsAndConditions" style="color:red" /></td>
    </tr>
    ...
  </table>
</h:form>

如果您只是想使复选框为必填,而与其他字段无关,请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.