1 Reply Latest reply on Apr 18, 2009 4:48 PM by rmertins

    Problems with value bindings in a dynmic form

    rmertins
      I am reading the forum all the time. Serveral other people have problems in the same way but the all end up by creating the form dynamicly. But this is not my problem. So i hope its ok that i post a new topic.

      Here my cenarion:

      1. I have two Entities
        a) Event which holds all metadata about an event and a collection FormElement Instances.
           FormElement is a Abstract base class which has implementaitons like TextField or DropDown.
           This colleciton represents the registration form.
        b) Participant holds all metadata about a participant like his id and so on. And a collection of ValueObjects.
           ValueObject is also a abstract base class which has implementations like StringValueObject and so on.

      A FormElement instance and a ValueObject instance are in relation over a key value which both share.

      2. My form is using ui:repeat to iterate over the formelements of the event.
         Here is an example for a textfield

      <ui:repeat value="#{eventManager.pageVariant.form.elements}" var="element">
                                      <s:div rendered="#{element.textField}">
                                              <ui:include src="/layout/booc/formelements/textfield.xhtml" >
                                                      <ui:param name="element" value="#{element.asTextField}" />
                                                      <ui:param name="vo" value="#{participantaction.getStringValue(element.name)}" />
                                              </ui:include>
                                      </s:div>
      </ui:repeat>
      THe base class formelement has two methods for every kind of implementation first a boolean one isTextField and a second one to get the correctly casted instance "asTextField".

      3. The participantaction component is managing the participant which is loged in and wants to register.
         This component has methods for any kind of Valueobject to get the correct value for a given FormELement key. If there is no value it creates an empty one and adds it to the participant.

      The include files like "testfield.xhtml" uses the param elment to configure the form element presentation in the gui and the ValueObject to bind the correct value.

      My problem is now that only the event and the participant are seam components and when a valueobject of an participant is changed this, change is not reflected in the participant object. It seems that the instance inside the participant is not the same like the one which is passed by jsf to the include file.

      In the moment i am trying to solve this problem by using valueChangeListener which get called for any form element on the page. Which works good, i get the value but i dont get access to the key.

      I tryed to add f:param and f:attribute to the inputText and or the ui:component in the include files. I can reach a param component from my valueChangeListener method but the value of the component is null. When i look in the attributes map of the UIParam component i can see the value is set to the EL expresseion #{vo} but its not evaluated.

      Has someone a hint or tipp how to solve this. I getting crazy. till this point everything was so nice and easy to implement thanks seam but now.

      thanks alot
        • 1. Re: Problems with value bindings in a dynmic form
          rmertins

          OK no one will help me. It's ok. I found a solution.


          1. Add an f:attribute to your inputfield:



          <f:attribute name="myKey" value="#{element.name" />




          2. In your valueChangelistener method do this:



          UIComponent comp = event.getComponent();
          ValueExpression ve = comp.getValueExpression("fieldKey");
          String fieldKey = (String) ve.getValue(FacesContext.getCurrentInstance().getELContext());
          



          3. enjoy the key u found and use it get your valueobject instance for the field.
          Set the value u got by event.getNewValue and make it persistent in the database.


          This way i have now a dynamic form based on my event entity and i have a value binding over the shared key value.


          I hope this will help someone ;)