1

我正在尝试为用户必须接受的条款和条件编写一个必需的复选框。我实际上不需要任何支持 bean,我只需要确保用户选中该框。

我尝试了以下方法,但似乎不起作用:

JSF:

<p:selectBooleanCheckbox required="true"> <f:validator validatorId="RequiredCheckboxValidator" /></p:selectBooleanCheckbox>

Java代码:

@FacesValidator("RequiredCheckboxValidator")
public class RequiredCheckboxValidator implements Validator {

    public void validate(FacesContext context, UIComponent component, Object value)
        throws ValidatorException
    {
        if (value.equals(Boolean.FALSE)) {
            String requiredMessage = ((UIInput) component).getRequiredMessage();

            if (requiredMessage == null) {
                Object label = component.getAttributes().get("label");
                if (label == null || (label instanceof String && ((String) label).length() == 0)) {
                    label = component.getValueExpression("label");
                }
                if (label == null) {
                    label = component.getClientId(context);
                }
                requiredMessage = MessageFormat.format(UIInput.REQUIRED_MESSAGE_ID, label);
            }

            throw new ValidatorException(
                new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage));
        }
    }

}
4

0 回答 0