0

I am using DynaForm in Primefaces extensions. I have generated a dynamic form, but the problem is how to recover the submitted data from the controller.

Here is how I am generating my form:

this is the controller side

    model = new DynaFormModel();  

    DynaFormRow row;

    allfieldMessageList=getAllfieldMessageList();

    for(FilledMessageField f:allfieldMessageList)
    {
        // 1. row  
        row = model.createRegularRow();  
        FilledMessageField fmf=new FilledMessageField();
        DynaFormLabel label11 = row.addLabel(fmf.getField().getName());  
        DynaFormControl control12 = row.addControl(fmf, "input");  
        label11.setForControl(control12);  
    }

and this is the web page side:

<h:form id="addMessageIDForm" rendered="true">
        <pe:dynaForm id="dynaForm" value="#{messageValuesController.model}" 
            var="data" widgetVar="dynaFormWidget" >     
            <pe:dynaFormControl type="input" for="txt" >
            <p:panelGrid columns="2">
                <p:inputText id="txt" value="#{data.value}"
                    required="#{data.required}" />
                    </p:panelGrid>
            </pe:dynaFormControl>
        </pe:dynaForm>
       <p:commandButton value="Submit" action="#{messageValuesController.saveMessageTemplate}" process="dynaForm"  />  
</h:form>

Please does any one have any idea about this ?

Thank you.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Badis
  • 27
  • 4

1 Answers1

0

As stated by the Primefaces Extensions Showcase, something like this will do the trick:

List<FilledMessageField> fields = new ArrayList<FilledMessageField>();  
for (DynaFormControl dynaFormControl : model.getControls())  
    if (dynaFormControl.getData() instanceof FilledMessageField)
        fields.add((FilledMessageField) dynaFormControl.getData());

But just out of curiosity, why do you iterate over FilledMessageField f:allfieldMessageList, if you don't use f? You may aswell just take a List< FilledMessageField> and add your model items to it, when populating the DynaForm controls, so you won't need to pull it out of the model with rather costly instanceof checks.

Note:
Also using DynaForm for an input only form seems like an overkill for me. You may just use a simple <ui:repeat> with <p:inputText>s inside, using your fields list as your backing collection.

Zhedar
  • 3,480
  • 1
  • 21
  • 44
  • Thank you for you answer, well about the variable thing it was just a copying mistake. I think I have found my problem, the postconstructer function get called and re-initialize my model whenever I submit my form even with ajax – Badis Jun 05 '15 at 16:27
  • I intend to add other input types as soon I get this one working. – Badis Jun 05 '15 at 16:29
  • @Badis consider making your Bean Viewscoped rather than requestscoped. – Zhedar Jun 05 '15 at 16:35
  • yes Zhedar that's what I have found on other stackoverflow questions, But this controller is already on @ViewScoped .. – Badis Jun 05 '15 at 16:40
  • thank you Zhedar I solved the problem I just had to remove the postconstruct function ... – Badis Jun 05 '15 at 16:58