Search code examples
jsfjakarta-eevalidationcustomvalidatorrequiredfieldvalidator

JSF Required=Yes not working inside a datatable?


I searched everywhere but could not find a solution to this. I am trying to used required=yes to validate whether a value is present or not. I am using it inside inputtext. The problem is it does not work inside a datatable. If I put the text box outside the datatable it works. I am using JSF 1.7 so I don't have the validateRequired tag from JSF 2.0.

I even used a validator class but it is still not working. Does anyone know why does required=yes or validator='validationClass' inside a inputtext inside a datatable is not working.

I appreciate the help.

Thanks.


Solution

  • First of all, the proper attribute values of the required attribute are the boolean values true or false, not a string value of Yes. It's an attribute which accepts a boolean expression.

    The following are proper usage examples:

    <h:inputText required="true" />
    <h:inputText required="#{bean.booleanValue}" />
    <h:inputText required="#{bean.stringValue == 'Yes'}" />
    

    As to the problem that it doesn't work inside a <h:dataTable>, that can happen when the datamodel is not been preserved properly (the datamodel is whatever the table retrieves in its value attribute). That can in turn happen when the managed bean is request scoped and doesn't prepare the datamodel during its (post)construction which causes that the datamodel is null or empty while JSF is about to gather, convert and validate the submitted values.

    You need to ensure that the datamodel is exactly the same during the apply request values phase of the form submit request as it was during the render response phase of the initial request to display the form with the table. An easy quick test is to put the bean in the session scope. If that fixes the problem, then you definitely need to rewrite the datamodel preserving logic. You could also use Tomahawk's <t:saveState> or <t:dataTable preserveDataModel="true"> to store the datamodel in the view scope (like as JSF2's new view scope is doing).

    Finally, JSF 1.7 doesn't exist. Perhaps you mean JSF 1.2?