Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Forms in Spring

I am trying to make a dynamic form using Spring forms. Basically, the form gets a title of learning activity and then there's the a button below it that says, "Add one more Learning Activity". This makes it possible for the user to add one more learning activity. I want him to be able to add as much as he likes.

I haven't tried this before so obviously I encountered errors with the first solution I thought of. I really had a feeling doing what I did will generate an error but just do drive home what I am trying to do, here's the code:

<script language="javascript">
 fields = 0;
 function addInput() {
      document.getElementById('text').innerHTML += "<form:input path='activity[fields++].activity'/><br />";
 }

<div id="text">
  <form:form commandName="course">
   Learning Activity 1 
   <form:input path="activity[0].activity"/>
    <input type="button" value="add activity" onclick="addInput()"/>
    <br/><br/>
   <input type="submit"/>
  </form:form>  
  <br/><br/>
 </div>
like image 370
Jeune Avatar asked Sep 17 '09 13:09

Jeune


People also ask

What is meant by dynamic forms?

What are Dynamic Forms? Dynamic forms are forms that change in real-time as people fill them out. They guide the user through the steps required to complete a form. Dynamic form designers can create forms that are data-driven and responsive to user inputs.

What is dynamic form design?

Dynamic forms are composite forms that allow you to present varying amounts of data to users. These forms change their layout according to the data they receive from the prefilling services at the time of rendering, so each separate request for form generation produces a form with a different length or content.

What Builder is used to create dynamic forms?

Make dynamic forms, and share or embed them in your site with Jotform's dynamic form builder — no coding required! Set up conditional logic, data verification, pre-populated form fields, and more in a few easy clicks.

How do I create a dynamic HTML form?

Approach 1: Use document. createElement() to create the new elements and use setAttribute() method to set the attributes of elements. Append these elements to the <form> element by appendChild() method. Finally append the <form> element to the <body> element of the document.


1 Answers

You can't use <form:input> within the javascript because is a jsp tag that runs on the server-side.

However, there's nothing magical about how an HTML input gets bound to a field in the Spring command object; it's just based on the name. So in your javascript, add a new <input type="text" name="activity[1].activity"> (for example -- obviously you'll increment the index).

Another option I've used for more complicated controls is to grab the HTML of the existing control (which was created using the Spring form:input tag) and clone it, replacing the indexes with the incremented number. This gets a lot easier if you use jQuery.

EDITED TO ADD: One issue that may cause you problems: you're appending your new input box to the end of your outer div ("text"), which means it's not inside the form tags. That won't work.

like image 78
Jacob Mattison Avatar answered Sep 21 '22 04:09

Jacob Mattison