3

I am trying to make a input form for answers in my application and I start with four "empty" answers which the view loops over and make input fields for. I have an add answer button which I add one question to the array of answers and then the view render the answers again, but now with an additional input field. The backing bean is viewscoped. However if I submit the form without pressing the add answer button it all works. The data is saved in the database. But if I add an answer after the four is filled out the last one does not get the data from the inputfield (answer.description). If I press the add answer first (without filling out any input fields) the data from the fields are not captured at all leaving all 5 empty so no data is saved in the database.

I have this in the form:

        <ui:repeat var="answer" value="#{bean.answers}">
            <div class="field">
                <h:outputLabel for="answerAlternative-#{answer.serialNumber}"
                    value="Svaralternativ #{answer.serialNumber}" />
                <h:inputText id="answerAlternative-#{answer.serialNumber}"
                    value="#{answer.description}" size="40" />
            </div>
        </ui:repeat>

This is the method for creating a new input field:

public String addAnswer() {
    if (answers.size() + 1 < 6) {
        Answer answer = new Answer();
        answer.setSerialNumber(answerSerialNumber + "");
        answerSerialNumber++;
        answers.add(answer);
    }

    return null;
}

Used for initializing the answers array with four empty input fields:

@PostConstruct
public void initBean() {
    answers = new ArrayList<Answer>();

    for (int i = 0; i < 4; i++) {
        addAnswer();
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • What JSF impl/version? Mojarra had for long serious problems with `` and partial state saving. Most of them should be fixed in current 2.1.4. MyFaces shouldn't have this kind of problems, you could give it a try as well. You can also try it with `` instead to exclude the one and other, it shouldn't have any problems in this scenario. – BalusC Nov 26 '11 at 17:24
  • Where can I check which version I use? – LuckyLuke Nov 26 '11 at 17:27
  • It's normally printed in webapp startup log. Check your IDE console. For Mojarra it look like `INFO: Initializing Mojarra 2.1.4 (SNAPSHOT 20111107) for context '/playground'`. – BalusC Nov 26 '11 at 17:27
  • INFO: Initializing Mojarra 2.1.3 (FCS b02) for context. Probably explains why? – LuckyLuke Nov 26 '11 at 17:29
  • I don't think. There was nothing fixed in 2.1.3 --> 2.1.4 with regard to `ui:repeat` and partial state saving. Major fixes were in the flash scope and in exception handling. Try `` or MyFaces. Sorry for the ambiguity of my first comment, I meant to say that until the current version some related issues have been fixed, not explicitly alone in 2.1.4. – BalusC Nov 26 '11 at 17:36
  • Using h:dataTable fixes the problem. But are there other tags that I can use that loop over some values without adding table tags? – LuckyLuke Nov 26 '11 at 18:14
  • Not in the standard JSF component set. Closest what you can get is Tomahawk's ``. – BalusC Nov 26 '11 at 18:23

1 Answers1

5

This look to match the current problems of <ui:repeat> in Mojarra. It is totally broken in Mojarra.

You have basically 2 options:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555