I am trying to create an input form for answers in my application, and I start with four βemptyβ answers that are scanned and enter input fields. I have a button to add an answer, in which I add one question to the answer array, and then the view displays the answers again, but now with an additional input field. The bean routine is being viewed. However, if I submit the form without clicking the add response button, everything will work. Data is stored in a database. But if I add the answer after completing four, the latter will not receive data from the input field (answer.description). If I first click on the add response (without filling in any input fields), the data from the fields is not written, leaving all 5 empty, so the data is not stored 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 of 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 to initialize an array of responses with four empty input fields:
@PostConstruct public void initBean() { answers = new ArrayList<Answer>(); for (int i = 0; i < 4; i++) { addAnswer(); } }
source share