You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Marcel Bonnet <ma...@gmail.com> on 2009/08/10 05:17:57 UTC

ListView inside Form: retrieving the listview's model

Hi everybody, I'm new in the mail-list. I've been studying the framework, I
even read the book Wicket in Action, but I'm having trouble using some kind
of repeater inside a Form, updating and validating the model.
What I'm trying to do is a ListView inside a Form: when the user change the
value of the dropdownchoice, it needs to fire a validator to verify if the
new choice is already in the list. If it is, the validator must call the
"error(validatable)" method from AbstractValidator because I don't want
repeated choices in the list.
First question, anybody knows a simple way to do this?
Second, if I'm on the right way with the code above, how can I get the
listview's model (with the updated values changed by the user, instead of
the default items rendered on the view on the first time) so I can pass this
updated model to my Validator (it needs to know the values in the list to
know wich is repeated).

I just supressed the part of the code that fires my feedback message ( a
kind of Label because I don't want a message per component, just a global
message)
Thanks for any help. Marcel.

private class InputForm extends Form
    {
        // holds NameWrapper elements
        private List<NameWrapper> data;

        public InputForm(String name, IFeedback feedback)
        {
            super(name);

            final SubmitLink update = new SubmitLink("update");
            add(update);

            // add some dummy data
            data = new ArrayList<NameWrapper>();
            data.add(new NameWrapper("one", 1, "default = 1", true));
            data.add(new NameWrapper("two", 2, "default = 2", false));
            data.add(new NameWrapper("three", 3, "default = 3", false));
            data.add(new NameWrapper("four", 4, "default = 4", true));
 final Model dataModel = new Model();
            dataModel.setObject(data);
            ListView listView = new ListView("list", dataModel)
            {

protected void populateItem(ListItem item)
                {

                    NameWrapper wrapper = (NameWrapper)item.getModelObject();

                    item.add(new Label("name", wrapper.getName()));
                    item.add(new CheckBox("check", new
PropertyModel(wrapper, "selected")));

                    DropDownChoice combo = new DropDownChoice("combo"
                            , new Model(wrapper)
                            , new ArrayList(Arrays.asList(new
NameWrapper(1000,"mil"), new NameWrapper(2000,"dois mil"), new
NameWrapper(3000,"três mil")))
                            , new ChoiceRenderer("comboText","comboId"));

                    combo.add(new
NameWrapperValidator((List<NameWrapper>)dataModel.getObject()));
                    item.add(combo);
                }
            };
                        *listView.setReuseItems(true); *//i read this line
is very important
                        add(listView);

Re: ListView inside Form: retrieving the listview's model

Posted by Marcel Bonnet <ma...@gmail.com>.
*SOLVED*.

I found what was wrong.
In case anyone needs a component like this, the main updated code is:

final Model dataModel = new Model();
dataModel.setObject(data);

ListView listView = new ListView("list", dataModel)
{

    protected void populateItem(ListItem item)
    {
        final IModel model = item.getModel();

        NameWrapper comboItem /*a kind of temp object for the
DropDownChoice:*/
            = new
NameWrapper(((NameWrapper)model.getObject()).getComboId(),((NameWrapper)model.getObject()).getComboText());
        final DropDownChoice combo = new DropDownChoice("combo"
                                    , new Model(comboItem)
                                    , CHOICES /* a list of possible choices
*/
                                    , new
ChoiceRenderer("comboText","comboId"));

        combo.add(new NameWrapperValidator(data));//data: the ListView's
object model. It will be iterated in my validator and call the
"error(validatable)" if there is more than one item of the current item. One
choice can be present only once in the whole list.
                        combo.add(new
AjaxFormComponentUpdatingBehavior("onchange"){
                            @Override
                            protected void onUpdate(AjaxRequestTarget
target) {
                                //the temp object will update the ListView's
Model (object named "data")

((NameWrapper)model.getObject()).setComboId(((NameWrapper)combo.getModelObject()).getComboId());

((NameWrapper)model.getObject()).setComboText(((NameWrapper)combo.getModelObject()).getComboText());
                            }
                        });
            item.add(combo);

    };
listView.setReuseItems(true);//so the list won't loose the new itens
add(listView);


>> --
>> View this message in context:
>> http://www.nabble.com/ListView-inside-Form%3A-retrieving-the-listview%27s-model-tp24893789p24922802.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

Re: ListView inside Form: retrieving the listview's model

Posted by bferr <bf...@juno.com>.
In your validator, invoke Form.visitFormComponents(FormComponent.IVisitor
visitor) 

The IVisitor will check getInput() vs the other
formComponent.getModelObject()



Marcel Bonnet wrote:
> 
> Thanks for helping.
> In fact I was trying to validate the ListView's model. I thought that each
> time we press the Submit button the new choice selected in each
> DropDownChoice in the ListView's rows would be submited and filled inside
> the Model of the ListView, meaning the model would now have the new values
> selected by the user. So I was trying to validate it's Model, because I
> want
> to find a way to tell the user if he selected the same choice twice or
> more.
> Each row of the ListView has a DropDownChoice but the form must not accept
> repeated choices.
> Now I'm not sure if the model would be updated or not...
> I had succes writing such repeater with a validator like this using
> RefreshingView instead of ListView. As I experienced, the model of the
> RefreshingView was updated when the for was submited, but only with
> TextField or CheckBox. When I changed the TextField by a DropwDownChoice,
> it
> just not worked, so I started working around with a ListView, but I'm
> having
> problems too.
> Any ideas, please for validating repeated choices inside a repeater?
> 
> 
> 2009/8/11 bferr <bf...@juno.com>
> 
>>
>> The model of your listView has to be LoadableDetachable so that the
>> listView
>> retrieves the new list values each time.   Also the setReuseItems() might
>> have to be false.   I don't think you're doing form validation within the
>> ListView correct?
>>
>>
>>
>>
>>
>> Marcel Bonnet wrote:
>> >
>> > Hi everybody, I'm new in the mail-list. I've been studying the
>> framework,
>> > I
>> > even read the book Wicket in Action, but I'm having trouble using some
>> > kind
>> > of repeater inside a Form, updating and validating the model.
>> > What I'm trying to do is a ListView inside a Form: when the user change
>> > the
>> > value of the dropdownchoice, it needs to fire a validator to verify if
>> the
>> > new choice is already in the list. If it is, the validator must call
>> the
>> > "error(validatable)" method from AbstractValidator because I don't want
>> > repeated choices in the list.
>> > First question, anybody knows a simple way to do this?
>> > Second, if I'm on the right way with the code above, how can I get the
>> > listview's model (with the updated values changed by the user, instead
>> of
>> > the default items rendered on the view on the first time) so I can pass
>> > this
>> > updated model to my Validator (it needs to know the values in the list
>> to
>> > know wich is repeated).
>> >
>> > I just supressed the part of the code that fires my feedback message (
>> a
>> > kind of Label because I don't want a message per component, just a
>> global
>> > message)
>> > Thanks for any help. Marcel.
>> >
>> > private class InputForm extends Form
>> >     {
>> >         // holds NameWrapper elements
>> >         private List<NameWrapper> data;
>> >
>> >         public InputForm(String name, IFeedback feedback)
>> >         {
>> >             super(name);
>> >
>> >             final SubmitLink update = new SubmitLink("update");
>> >             add(update);
>> >
>> >             // add some dummy data
>> >             data = new ArrayList<NameWrapper>();
>> >             data.add(new NameWrapper("one", 1, "default = 1", true));
>> >             data.add(new NameWrapper("two", 2, "default = 2", false));
>> >             data.add(new NameWrapper("three", 3, "default = 3",
>> false));
>> >             data.add(new NameWrapper("four", 4, "default = 4", true));
>> >  final Model dataModel = new Model();
>> >             dataModel.setObject(data);
>> >             ListView listView = new ListView("list", dataModel)
>> >             {
>> >
>> > protected void populateItem(ListItem item)
>> >                 {
>> >
>> >                     NameWrapper wrapper =
>> > (NameWrapper)item.getModelObject();
>> >
>> >                     item.add(new Label("name", wrapper.getName()));
>> >                     item.add(new CheckBox("check", new
>> > PropertyModel(wrapper, "selected")));
>> >
>> >                     DropDownChoice combo = new DropDownChoice("combo"
>> >                             , new Model(wrapper)
>> >                             , new ArrayList(Arrays.asList(new
>> > NameWrapper(1000,"mil"), new NameWrapper(2000,"dois mil"), new
>> > NameWrapper(3000,"três mil")))
>> >                             , new
>> ChoiceRenderer("comboText","comboId"));
>> >
>> >                     combo.add(new
>> > NameWrapperValidator((List<NameWrapper>)dataModel.getObject()));
>> >                     item.add(combo);
>> >                 }
>> >             };
>> >                         *listView.setReuseItems(true); *//i read this
>> line
>> > is very important
>> >                         add(listView);
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/ListView-inside-Form%3A-retrieving-the-listview%27s-model-tp24893789p24922802.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/ListView-inside-Form%3A-retrieving-the-listview%27s-model-tp24893789p24943860.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: ListView inside Form: retrieving the listview's model

Posted by Marcel Bonnet <ma...@gmail.com>.
Thanks for helping.
In fact I was trying to validate the ListView's model. I thought that each
time we press the Submit button the new choice selected in each
DropDownChoice in the ListView's rows would be submited and filled inside
the Model of the ListView, meaning the model would now have the new values
selected by the user. So I was trying to validate it's Model, because I want
to find a way to tell the user if he selected the same choice twice or more.
Each row of the ListView has a DropDownChoice but the form must not accept
repeated choices.
Now I'm not sure if the model would be updated or not...
I had succes writing such repeater with a validator like this using
RefreshingView instead of ListView. As I experienced, the model of the
RefreshingView was updated when the for was submited, but only with
TextField or CheckBox. When I changed the TextField by a DropwDownChoice, it
just not worked, so I started working around with a ListView, but I'm having
problems too.
Any ideas, please for validating repeated choices inside a repeater?


2009/8/11 bferr <bf...@juno.com>

>
> The model of your listView has to be LoadableDetachable so that the
> listView
> retrieves the new list values each time.   Also the setReuseItems() might
> have to be false.   I don't think you're doing form validation within the
> ListView correct?
>
>
>
>
>
> Marcel Bonnet wrote:
> >
> > Hi everybody, I'm new in the mail-list. I've been studying the framework,
> > I
> > even read the book Wicket in Action, but I'm having trouble using some
> > kind
> > of repeater inside a Form, updating and validating the model.
> > What I'm trying to do is a ListView inside a Form: when the user change
> > the
> > value of the dropdownchoice, it needs to fire a validator to verify if
> the
> > new choice is already in the list. If it is, the validator must call the
> > "error(validatable)" method from AbstractValidator because I don't want
> > repeated choices in the list.
> > First question, anybody knows a simple way to do this?
> > Second, if I'm on the right way with the code above, how can I get the
> > listview's model (with the updated values changed by the user, instead of
> > the default items rendered on the view on the first time) so I can pass
> > this
> > updated model to my Validator (it needs to know the values in the list to
> > know wich is repeated).
> >
> > I just supressed the part of the code that fires my feedback message ( a
> > kind of Label because I don't want a message per component, just a global
> > message)
> > Thanks for any help. Marcel.
> >
> > private class InputForm extends Form
> >     {
> >         // holds NameWrapper elements
> >         private List<NameWrapper> data;
> >
> >         public InputForm(String name, IFeedback feedback)
> >         {
> >             super(name);
> >
> >             final SubmitLink update = new SubmitLink("update");
> >             add(update);
> >
> >             // add some dummy data
> >             data = new ArrayList<NameWrapper>();
> >             data.add(new NameWrapper("one", 1, "default = 1", true));
> >             data.add(new NameWrapper("two", 2, "default = 2", false));
> >             data.add(new NameWrapper("three", 3, "default = 3", false));
> >             data.add(new NameWrapper("four", 4, "default = 4", true));
> >  final Model dataModel = new Model();
> >             dataModel.setObject(data);
> >             ListView listView = new ListView("list", dataModel)
> >             {
> >
> > protected void populateItem(ListItem item)
> >                 {
> >
> >                     NameWrapper wrapper =
> > (NameWrapper)item.getModelObject();
> >
> >                     item.add(new Label("name", wrapper.getName()));
> >                     item.add(new CheckBox("check", new
> > PropertyModel(wrapper, "selected")));
> >
> >                     DropDownChoice combo = new DropDownChoice("combo"
> >                             , new Model(wrapper)
> >                             , new ArrayList(Arrays.asList(new
> > NameWrapper(1000,"mil"), new NameWrapper(2000,"dois mil"), new
> > NameWrapper(3000,"três mil")))
> >                             , new ChoiceRenderer("comboText","comboId"));
> >
> >                     combo.add(new
> > NameWrapperValidator((List<NameWrapper>)dataModel.getObject()));
> >                     item.add(combo);
> >                 }
> >             };
> >                         *listView.setReuseItems(true); *//i read this
> line
> > is very important
> >                         add(listView);
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/ListView-inside-Form%3A-retrieving-the-listview%27s-model-tp24893789p24922802.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: ListView inside Form: retrieving the listview's model

Posted by bferr <bf...@juno.com>.
The model of your listView has to be LoadableDetachable so that the listView
retrieves the new list values each time.   Also the setReuseItems() might
have to be false.   I don't think you're doing form validation within the
ListView correct?





Marcel Bonnet wrote:
> 
> Hi everybody, I'm new in the mail-list. I've been studying the framework,
> I
> even read the book Wicket in Action, but I'm having trouble using some
> kind
> of repeater inside a Form, updating and validating the model.
> What I'm trying to do is a ListView inside a Form: when the user change
> the
> value of the dropdownchoice, it needs to fire a validator to verify if the
> new choice is already in the list. If it is, the validator must call the
> "error(validatable)" method from AbstractValidator because I don't want
> repeated choices in the list.
> First question, anybody knows a simple way to do this?
> Second, if I'm on the right way with the code above, how can I get the
> listview's model (with the updated values changed by the user, instead of
> the default items rendered on the view on the first time) so I can pass
> this
> updated model to my Validator (it needs to know the values in the list to
> know wich is repeated).
> 
> I just supressed the part of the code that fires my feedback message ( a
> kind of Label because I don't want a message per component, just a global
> message)
> Thanks for any help. Marcel.
> 
> private class InputForm extends Form
>     {
>         // holds NameWrapper elements
>         private List<NameWrapper> data;
> 
>         public InputForm(String name, IFeedback feedback)
>         {
>             super(name);
> 
>             final SubmitLink update = new SubmitLink("update");
>             add(update);
> 
>             // add some dummy data
>             data = new ArrayList<NameWrapper>();
>             data.add(new NameWrapper("one", 1, "default = 1", true));
>             data.add(new NameWrapper("two", 2, "default = 2", false));
>             data.add(new NameWrapper("three", 3, "default = 3", false));
>             data.add(new NameWrapper("four", 4, "default = 4", true));
>  final Model dataModel = new Model();
>             dataModel.setObject(data);
>             ListView listView = new ListView("list", dataModel)
>             {
> 
> protected void populateItem(ListItem item)
>                 {
> 
>                     NameWrapper wrapper =
> (NameWrapper)item.getModelObject();
> 
>                     item.add(new Label("name", wrapper.getName()));
>                     item.add(new CheckBox("check", new
> PropertyModel(wrapper, "selected")));
> 
>                     DropDownChoice combo = new DropDownChoice("combo"
>                             , new Model(wrapper)
>                             , new ArrayList(Arrays.asList(new
> NameWrapper(1000,"mil"), new NameWrapper(2000,"dois mil"), new
> NameWrapper(3000,"três mil")))
>                             , new ChoiceRenderer("comboText","comboId"));
> 
>                     combo.add(new
> NameWrapperValidator((List<NameWrapper>)dataModel.getObject()));
>                     item.add(combo);
>                 }
>             };
>                         *listView.setReuseItems(true); *//i read this line
> is very important
>                         add(listView);
> 
> 

-- 
View this message in context: http://www.nabble.com/ListView-inside-Form%3A-retrieving-the-listview%27s-model-tp24893789p24922802.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org