org.omnifaces.validator
Class RequiredCheckboxValidator

java.lang.Object
  extended by org.omnifaces.validator.RequiredCheckboxValidator
All Implemented Interfaces:
java.util.EventListener, javax.faces.validator.Validator

public class RequiredCheckboxValidator
extends java.lang.Object
implements javax.faces.validator.Validator

This validator solves the broken required="true" attribute of the <h:selectBooleanCheckbox>. If you want to require the user to tick the desired checkbox, you would expect that setting required="true" is sufficient. But it is not, the validation wil always pass.

As for every other UIInput component the default required="true" validator would only check if the value is actually filled and been sent to the server side, i.e. the value is not null nor empty. In case of a <h:selectBooleanCheckbox>, which accepts Boolean or boolean properties only, JSF EL will coerce the unchecked value to Boolean.FALSE during apply request values phase, right before validations phase. This value is not null nor empty! Thus, the required attribute of the <h:selectBooleanCheckbox> is fairly pointless. It would always pass the validation and thus never display the desired required message in case of an unticked checkbox.

To get it to work anyway, register this validator on validator ID of omnifaces.RequiredCheckboxValidator as follows:

 <h:selectBooleanCheckbox id="agree" value="#{bean.agree}" requiredMessage="You must agree!">
   <f:validator validatorId="omnifaces.RequiredCheckboxValidator" />
 </h:selectBooleanCheckbox>
 

The validator will use the message as specified in requiredMessage. If it's absent, then it will use the default required message as specified in custom <message-bundle> in faces-config.xml. If it's absent, then it will default to "{0}: a tick is required".

Author:
Bauke Scholtz

Field Summary
 
Fields inherited from interface javax.faces.validator.Validator
NOT_IN_RANGE_MESSAGE_ID
 
Constructor Summary
RequiredCheckboxValidator()
           
 
Method Summary
 void validate(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, java.lang.Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RequiredCheckboxValidator

public RequiredCheckboxValidator()
Method Detail

validate

public void validate(javax.faces.context.FacesContext context,
                     javax.faces.component.UIComponent component,
                     java.lang.Object value)
              throws javax.faces.validator.ValidatorException
Specified by:
validate in interface javax.faces.validator.Validator
Throws:
javax.faces.validator.ValidatorException