Sometime you need to dynamically generate a form without knowing how many fields it will be required (i.e. when your form is driven by a configuration or by some properties). The problem is to draw the form and, return the values to the Controller, and recognize the couples Field name / Field value after the submit..
You can easily get solve this problem just adding HashMap which will hold the key-value pair data to the DataModel.
Supposing your configuration says: you have to draw two fields and these are the name, you UI will be something like*:
<c:forEach items="${newRequest.fields}" var="field">
<f:input type="text" path="rawFields['${field.field_id}']" class="form-control validate[groupRequired[mandatoryField]]" /> (R)
</c:forEach> |
<c:forEach items="${newRequest.fields}" var="field">
<f:input type="text" path="rawFields['${field.field_id}']" class="form-control validate[groupRequired[mandatoryField]]" /> (R)
</c:forEach>
When you submit the form, the values and the key for the dynamic fields will be filled.
* newRequest is the DataModel you are passing and fields is the list of Fields that user will fill with data, like that:
public class Request {
/** Request type */
private int templateRequest;
// ***** Input field *****
List<RequestField> fields = new ArrayList<RequestField>();
private HashMap<String, Object> rawFields = new HashMap<String, Object>();
[Setters and getters]
} |
public class Request {
/** Request type */
private int templateRequest;
// ***** Input field *****
List<RequestField> fields = new ArrayList<RequestField>();
private HashMap<String, Object> rawFields = new HashMap<String, Object>();
[Setters and getters]
}