validate input field in jsf datatable
I have input field in jsf datatable which i am validating and adding error message but error message doesn't get displayed. What is the best approach in validating input field in jsf datatable and adding message.
<h:inputTextarea id="t开发者_如何学Goextarea1" styleClass="inputTextarea" value="#{varreviewList.comment}">
<f:attribute name="msgRef" value="Valid comment is Required."/>
</h:inputTextarea>
<h:message styleClass="message" for="textarea1" id="textarea1Msg"></h:message>
Any help is great.
Thanks, Sujit
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.
This is working for me inside a datatable:
<ice:inputText id="inputTextt" field="#{bean[fieldValue]}" required="true"/>
<ice:message for="inputTextt"></ice:message>
精彩评论