click-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bob Schellink <sab...@gmail.com>
Subject Re: Question about onInit() and listener invokation - where to initialize
Date Thu, 29 Apr 2010 23:03:59 GMT
Hi Dipita,

You are correct in that the Select field will need to be bound to its request value *before*
the
Form is initialized. The normal life cycle of Click is to bind request values during the onProcess
event, which is *after* the Form was initialized. One way to retrieve the Select request value
is
from the Context object.


  public void onInit() {
    getContext().getRequestParameter("mySelect"); // <- mySelect will be the Select field
name
  }


or if you have access to the Select, do this:

  public void onInit() {
    Select select = new Select("mySelect");
    select.add("option 1");
    select.add("option 2");
    ...

    select.bindRequestValue(); // <- will bind the select to its incoming value
    String parameter = select.getValue();
  }


In your example I'm unsure if the Select is created inside the addFieldsBasedonParameter or
before.
I assume it is created before. So you can modify it as follows:

  public initailizeFields() {
    select = new Select("myselect");
    select.add("option 1");
    ...
    select.bindRequestValue();
    String parameter = select.getValue();

    myForm = new Form("myForm");
    addFieldsBasedonParameter(parameter);
  }


In the upcoming 2.2.0 we have added helper methods to bind and validate fields and forms to
cater
for this exact scenario you describe above.

Does that help?

Kind regards

Bob

On 30/04/2010 04:03, dshah [via click] wrote:
> Hi,
> I have read all the forum questions on this topic and am unsure the
> correct way of implementing what I want to do:
> 
> I have a page that contains a panel.  In the onInit() method of the
> page, I create the panel and initialize a Form in that Panel.. as follows:
> 
> public class myPage extends Page {
>   Panel myPanel;
> 
>   public onInit() {
>       myPanel = new abcPanel(...);
>       myPanel.initializeFields();
>       add(myPanel);
>   }
>   ...
> }
> 
> public class myPanel extends Panel {
>    Form myForm;
>    
>    public myPanel(..) {
>        //do some construction work
>   }
>   public initailizeFields() {
>      myForm = new Form("myForm");
>      addFieldsBasedonParameter(parameter) // this will populate the
> various fields for the form based on the parameter.  The default
> parameter only has a small subset of the fields..
>   }
> }
> 
> Now my problem is that my default parameter (which is used to figure out
> which fields should be created on the form) only have a subset of
> fields.  Depending on this parameter, the fields could be more or less..
> This parameter is a Select Control which is added to the form. The user
> can select this parameter (and if the selection is changed, I reload the
> page with the parameter) As I said some of these parameters will load
> more fields..
> 
> Now my problem is that before the onSave (aka listener method of the
> form click event) is called, the onInit() is called - which recreates
> the Form with the default paramter (although this parameter was changed
> when the user changed the selection...) So by the time the onSave
> listener is invoked, some fields that should be on the Form are not
> there (because the form was recreated with default parameters in the
> onInit() method..)
> 
> What is the best way to handle this  - the form fields have to be
> generated dynamically based on the user's selection..
> 
> Any help is appreciated.
> Thanks!
> Dipita
> 
> ------------------------------------------------------------------------
> This email was sent by dshah
> <http://click.1134972.n2.nabble.com/user/UserRootNodes.jtp?user=373530>
> (via Nabble)
> Your replies will appear at
> http://click.1134972.n2.nabble.com/Question-about-onInit-and-listener-invokation-where-to-initialize-tp4981417p4981417.html
> To receive all replies by email, subscribe to this discussion
> <http://click.1134972.n2.nabble.com/subscriptions/Subscribe.jtp?code=c2Fib2IxQGdtYWlsLmNvbXw0OTgxNDE3fC00OTUyOTIwOTU=>
> 


Mime
View raw message