public class RequiredCheckboxValidator extends Object implements Validator
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".
NOT_IN_RANGE_MESSAGE_ID
Constructor and Description |
---|
RequiredCheckboxValidator() |
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
validate
in interface Validator
ValidatorException