The code as far looks fine, assuming they're both placed inside a <h:column>
(not necessarily the same column though), so the problem lies somewhere else.
If you after all just want to validate if the value is filled or not, then you just need to add required="true"
to the component in question.
<h:inputSomething required="true" />
If you want to override the default required message, either use requiredMessage
attribute (since JSF 1.2 only)
<h:inputSomething required="true" requiredMessage="Please enter value!" />
...or supply a custom messages.properties
in the application
's message-bundle
in faces-config.xml
with the following line
javax.faces.component.UIInput.REQUIRED = Please enter {0}.
...where {0}
is controllable by (since JSF 1.2 only, else it's the clientId
).
<h:inputSomething label="This field" />
Or if you after all want a custom validator, then you need to implement javax.faces.validator.Validator
, register it as validator
in faces-config.xml
<validator>
<validator-id>myValidator</validator-id>
<validator-class>com.example.MyValidator</validator-class>
</validator>
...and attach it to the input component by validator
attribute
<h:inputSomething validator="myValidator" />
... or f:validator
facet (so that you can attach multiple validators to one component)
<h:inputSomething>
<f:validator validatorId="myValidator" />
</h:inputSomething>
Inside the implemented validate()
method, just throw ValidatorException
with the desired FacesMessage
whenever needed.
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (value does not match some condition) {
throw new ValidatorException(new FacesMessage("Please enter valid value"));
}
}
It will then automatically show up in the associated message component.