views:

104

answers:

1

Say I have a editable datatable, with a custom converter that throws an exception if the edited field is somehow wrong, how would I display the error message in the corresponding row of the datatable?

here's some code, as simple as I can make it.

<h:messages />
<h:datatable>
    <h:column>
        <h:inputText value="#{bean.property}">
            <f:converter converterId="PropertyConverter" />
        </h:inputText>
    </h:column>
</h:datatable>

If there's an error in one row, how would I place the error message in that row. I can obviously have a column for errors, but how do I target the corresponding row?

+2  A: 

Just add a <h:message /> component inside the same table whose for attribute points to the id of the UIInput component in question.

For example:

<h:datatable>
    <h:column>
        <h:inputText id="someId" value="#{bean.property}">
            <f:converter converterId="PropertyConverter" />
        </h:inputText>
    </h:column>
    <h:column>
        <h:message for="someId" />
    </h:column>
</h:datatable>

or if you want to have it directly after the input element inside the same column:

<h:datatable>
    <h:column>
        <h:inputText id="someId" value="#{bean.property}">
            <f:converter converterId="PropertyConverter" />
        </h:inputText>
        <h:message for="someId" />
    </h:column>
</h:datatable>

No additional code is needed, just throw the ConverterException as usual. JSF will take care about the magic (displaying the message in the right row).

BalusC
Excellent, thanks again BalusC.
Bedwyr Humphreys