3

I have a p:inputText field inside a dataTable and I'm trying to set its identifier dynamically.

However I get the following error:

component identifier must not be a zero-length String

None of the String used as id are null or of zero-length

<h:form id="updateform">
            <p:dataTable id="updatetable" value="#{EditingBean.row}" var="column"
                                 style="width: 983px; overflow-x: auto; white-space: normal;">
                <f:facet name="header">
                    <h:outputText value="#{EditingBean.currentStatement.statementName}" />
                </f:facet>
                <p:column rendered="#{column.display}" style="white-space: normal;">
                    <h:outputText value="#{column.alias}" />
                </p:column>
                <p:column rendered="#{column.display}" style="white-space: normal;">
                    <p:inputText id="#{column.name}" value="#{column.value}" />
                </p:column>
                <f:facet name="footer" style="text-align: right;">
                    <h:commandButton value="Update" action="#{EditingBean.update()}"
                        ajax="false" />
                </f:facet>
            </p:dataTable>
        </h:form>
noisegrrrl
  • 149
  • 1
  • 13
  • Check the value of #{colum.name} for every object in your #{EditingBean.row} object again. If even one name attribute is null or "" (an empty string), you'll get the error. – Manuel Aug 06 '13 at 13:38
  • I have, absolutely none of them are null or "". – noisegrrrl Aug 06 '13 at 13:53
  • Here are the values : mep_ident, mep_ptrpartident, mep_ptrpasident, mep_code_doc, mep_production – noisegrrrl Aug 06 '13 at 13:57

1 Answers1

2

The id attribute must be set during view build time.

However, as #{column} is declared as <h:dataTable var>, it's only available during view render time. As it runs after view build time, it's thus null during view build time.

I'm not sure about the concrete functional requirement for which you thought that doing this would be the right solution. The code is not self-documenting enough to properly guess it. So it's hard to give a suitable answer. In any case, you've basically 2 options:

  1. Use an iterating tag which runs during view build time, such as JSTL <c:forEach>.

    <c:forEach items="#{EditingBean.row}" var="column">
        ...
        <p:inputText id="#{column.name}" ... />
    
  2. You perhaps actually don't need a dynamic ID at all. Give it a fixed ID.

    <p:inputText id="name" ... />
    

    JSF will already take care of generating unique IDs (prefixed with row index) in HTML output. Rightclick page and do View Source to see it yourself.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I need to use the name for validating the input as I don't know beforehand what type the input is supposed to be. – noisegrrrl Aug 06 '13 at 14:24
  • Just set a CSS class on `styleClass` like `styleClass="phone"`, `styleClass="url"`, `styleClass="email"`, etc. Those kind of inputs can impossibly be unique anyway, so your `id` approach makes in first place already no utter sense. With jQuery you can easily grab them as in `$(".phone")`, `$(".email")`, etc. – BalusC Aug 06 '13 at 14:24
  • A column has usually more than one row, right? So you end up with as many inputs in one column as many rows are, right? So with e.g. 10 rows you end up having 10 phone inputs, etc. – BalusC Aug 06 '13 at 14:27
  • That's fine. This still doesn't mean that those inputs are unique in HTML context. There are still multiple of them, only conditionally shown/hidden. A CSS class is therefore the right and most easy solution. – BalusC Aug 06 '13 at 14:29
  • My problem though is that I then need to access the field when validating (I'm using ``) Is it possible to access components by StyleClass like it is by id? – noisegrrrl Aug 06 '13 at 14:33
  • Just set the validator on the input itself? Why do you keep trying to solve things the hard way? – BalusC Aug 06 '13 at 14:33