tags:

views:

921

answers:

1

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="textarea1" 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

+1  A: 

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.

BalusC
Hi BalusC, Thanks for the Reply i wrote custom validator but my validator is not getting invoked. When i click command button it goes into action. Nothing is required field in my page. I need check validation on some condition. I registered validator in faces config and specied in jsf page something like this. <h:inputSomething> <f:validator validatorId="myValidator" /> </h:inputSomething>
The attached validator will **only** be invoked when the value is **not** null or empty. For that part you normally use the `required` attribute. You may need to elaborate more about the exact functional requirement so that we can give better suited answers. Maybe you want to validate on a certain condition/dependency?
BalusC
Thanks again i got validator working. My requirement is i have to check for dependency and display error message in datatable row. I am able to add error message and show messages globally but not in a datatable row.Example: if a row 5 COMMENT is required than i have to show error message in row5 column3(comment goes in column3) of datatable.
Then just put the `<h:message>` right there.
BalusC
I already have <h:message> but message doesn't get displayed.<h:inputTextarea id="textarea1" 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>
Thanks BalusC for you responses but it looks like to me that inside datatable h:message association with inputfields is not working properly. I tried using Custom validator and server side validation. I am adding messages correctly but when it renders the message is not getting displayed. Thanks again for comments.