You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Azzeddine Daddah <wa...@gmail.com> on 2009/02/28 17:17:48 UTC

DropDownChoice always sets its model to null

Hi,
I'm trying to use a DropDownChoice to display and store the selected
category in the database. The value selected in the drop down is correctly
set, but when I look to the model (Category) of this drop down, it returns
always null. Do I do something wrong? Below some code.

public AddRecipeForm(String id) {
        super(id);
        setModel(new Model<Recipe>(new Recipe()));
        ...
        add(new CategoriesDropDown("categories",
recipe.getCategory()).setRequired(true));
}
...
public void onSubmit() {
    RecipeService recipeService = RecipeService.getInstance();
    Recipe recipe = form.getModelObject();
    Category cat = recipe.getCategory(); // This returns always null
}

...

private class CategoriesDropDown extends DropDownChoice<Category> {
        public CategoriesDropDown(String id, Category category) {
            super(id, new Model<Category>(category),
RecipeService.getInstance().getAllPersistentRecipeCategories(), new
IChoiceRenderer<Category>() {
                @Override
                public String getIdValue(Category category, int index) {
                    return category.getName();
                }

                @Override
                public Object getDisplayValue(Category category) {
                    return category.getName();
                }
            });
        }
    }


Kind regards,

Hbiloo

Re: DropDownChoice always sets its model to null

Posted by Azzeddine Daddah <wa...@gmail.com>.
The category is still null. Could someone tell me where it goes wrong?
Thanks,

Hbiloo


On Sat, Feb 28, 2009 at 6:39 PM, Azzeddine Daddah <wa...@gmail.com>wrote:

> The Recipe object is indeed serializable. Below some code from
> the AddRecipeForm and Recipe:
>
> public class Recipe implements Serializable {
>     public static final int NAME_LENGTH           = 50;
>     public static final int DIFFICULTY_LENGTH   = 6;
>     public static final int INTRODUCTION_LENGTH = 150;
>
>     @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
> "recipe_seq")
>     private Long id;
>     ...
>
>     @ManyToOne
>     @org.hibernate.annotations.Cascade(CascadeType.SAVE_UPDATE)
>     @JoinColumn(name = "CATEGORY_ID")
>     @ForeignKey(name = "FK_RECIPE_CATEGORY")
>     private Category category;
>     ...
>
>     public Category getCategory() {
>         return category;
>     }
>
>     public void setCategory(Category category) {
>         assert category != null;
>         this.category = category;
>     }
> }
>
> public class AddRecipeForm extends Form<Recipe> {
>     private static final Logger log =
> LoggerFactory.getLogger(AddRecipeForm.class);
>
>     private final List<FileUpload> uploads = new ArrayList<FileUpload>();
>     private Recipe recipe = new Recipe();
>
>     /**
>      * Creates a new {@link AddRecipeForm}.
>      *
>      * @param id form id; may not be <code>null</code>
>      */
>     public AddRecipeForm(String id) {
>         super(id);
>         setModel(new Model<Recipe>(recipe));
>
>         add(new FeedbackPanel("feedback"));
>         addRecipeInfoFormPart();
>         ...
>         add(createSubmitLink(this));
>     }
>
>     private void addRecipeInfoFormPart() {
>         add(new RequiredTextField<String>("recipeTitle", new
> PropertyModel<String>(recipe, "name"))
>                 .add(StringValidator.lengthBetween(5,
> Recipe.NAME_LENGTH)));
>         add(new CategoriesDropDown("categories", new
> PropertyModel<Category>(recipe, "category")).setRequired(true));
>         add(new RecipeDifficultiesDropDown("difficulties",
> recipe).setRequired(true));
>         add(new TextArea<String>("introduction", new
> PropertyModel<String>(recipe, "introduction")));
>     }
>    ...
>     private StyledSubmitLink createSubmitLink(final Form<Recipe> form) {
>         StyledSubmitLink submitLink = new StyledSubmitLink("submitLink") {
>             @Override
>             public void onSubmit() {
>                 RecipeService recipeService = RecipeService.getInstance();
>                 Recipe recipe = form.getModelObject();
>                 Category recipeCategory =
> recipeService.findCategoryByCategoryName(recipe.getCategory().getName());
>                 Set<Tag> persistentTags = new HashSet<Tag>();
>                 for (Tag tag : recipe.getTags()) {
>                     Tag persistentTag =
> recipeService.findTagByTagName(tag.getName());
>                     persistentTags.add(persistentTag == null ? tag :
> persistentTag);
>                 }
>                 log.debug(String.format("from persisted cat: %s and from
> form %s", recipeCategory.getName(), recipe.getCategory().getName()));
>                 Status persistentStatus =
> recipeService.findStatusByStatusName("NEW");
>                 Recipe newRecipe = new Recipe(recipeCategory,
>                         recipe.getDifficulty(),
>                         recipe.getIntroduction(),
>                         recipe.getMethod(),
>                         recipe.getName(),
>                         new Integer(0),
>                         UserUtils.getUser(),
>                         recipe.getPreparationTime(),
>                         recipe.getRecipeImages(),
>                         new
> HashSet<RecipeIngredient>(recipe.getRecipeIngredients()),
>                         recipe.getServings(),
>                         persistentStatus == null ? new Status("NEW", "New")
> : persistentStatus,
>                         persistentTags);
>
>                 uploadImages();
>
>                 recipeService.insertNewRecipe(newRecipe);
>                 setResponsePage(new
> SuccessPage(getString("message.success.addrecipe.title"),
>                         getString("message.success.addrecipe.body")));
>             }
>         };
>         submitLink.add(new Label("submit", new
> ResourceModel("form.addrecipe.add")));
>         return submitLink;
>     }
>
>     private class CategoriesDropDown extends DropDownChoice<Category> {
>         public CategoriesDropDown(String id, IModel<Category> model) {
>             super(id, model,
> RecipeService.getInstance().getAllPersistentRecipeCategories());
>             setChoiceRenderer(new IChoiceRenderer<Category>() {
>                 @Override
>                 public String getIdValue(Category category, int index) {
>                     return category.getName();
>                 }
>
>                 @Override
>                 public Object getDisplayValue(Category category) {
>                     return category.getName();
>                 }
>             });
>         }
>     }
>
>     private class RecipeDifficultiesDropDown extends
> DropDownChoice<RecipeDifficulty> {
>         public RecipeDifficultiesDropDown(String id, Recipe recipe) {
>             super(id, new PropertyModel<RecipeDifficulty>(recipe,
> "difficulty"), Arrays.asList(RecipeDifficulty.values()),
>                     new IChoiceRenderer<RecipeDifficulty>() {
>                         @Override
>                         public Object getDisplayValue(RecipeDifficulty
> recipeDifficulty) {
>                             return
> recipeDifficulty.getFormattedDifficulty();
>                         }
>
>                         @Override
>                         public String getIdValue(RecipeDifficulty
> recipeDifficulty, int index) {
>                             return recipeDifficulty.getName();
>                         }
>             });
>         }
>     }
> }
>
> On Sat, Feb 28, 2009 at 6:24 PM, Igor Vaynberg <ig...@gmail.com>wrote:
>
>> where is the code that creates your form? and is Recipe serializable?
>>
>> -igor
>>
>> On Sat, Feb 28, 2009 at 8:58 AM, Azzeddine Daddah <wa...@gmail.com>
>> wrote:
>> > Thanks Igor,
>> > I've already tried that but still have the same problem.
>> > add(new CategoriesDropDown("categories", new
>> PropertyModel<Category>(recipe,
>> > "category")).setRequired(true));
>> > ...
>> > private class CategoriesDropDown extends DropDownChoice<Category> {
>> >        public CategoriesDropDown(String id, IModel<Category> model) {
>> >            super(id, model,
>> > RecipeService.getInstance().getAllPersistentRecipeCategories(),
>> >                    new IChoiceRenderer<Category>() {
>> >                @Override
>> >                public String getIdValue(Category category, int index) {
>> >                    return category.getName();
>> >                }
>> >
>> >                @Override
>> >                public Object getDisplayValue(Category category) {
>> >                    return category.getName();
>> >                }
>> >            });
>> >        }
>> >    }
>> >
>> >
>> >
>> > On Sat, Feb 28, 2009 at 5:54 PM, Igor Vaynberg <igor.vaynberg@gmail.com
>> >wrote:
>> >
>> >> use a property model instead of recipe.getcategories()
>> >>
>> >> -igor
>> >>
>> >> On Sat, Feb 28, 2009 at 8:17 AM, Azzeddine Daddah <
>> waarheid08@gmail.com>
>> >> wrote:
>> >> > Hi,
>> >> > I'm trying to use a DropDownChoice to display and store the selected
>> >> > category in the database. The value selected in the drop down is
>> >> correctly
>> >> > set, but when I look to the model (Category) of this drop down, it
>> >> returns
>> >> > always null. Do I do something wrong? Below some code.
>> >> >
>> >> > public AddRecipeForm(String id) {
>> >> >        super(id);
>> >> >        setModel(new Model<Recipe>(new Recipe()));
>> >> >        ...
>> >> >        add(new CategoriesDropDown("categories",
>> >> > recipe.getCategory()).setRequired(true));
>> >> > }
>> >> > ...
>> >> > public void onSubmit() {
>> >> >    RecipeService recipeService = RecipeService.getInstance();
>> >> >    Recipe recipe = form.getModelObject();
>> >> >    Category cat = recipe.getCategory(); // This returns always null
>> >> > }
>> >> >
>> >> > ...
>> >> >
>> >> > private class CategoriesDropDown extends DropDownChoice<Category> {
>> >> >        public CategoriesDropDown(String id, Category category) {
>> >> >            super(id, new Model<Category>(category),
>> >> > RecipeService.getInstance().getAllPersistentRecipeCategories(), new
>> >> > IChoiceRenderer<Category>() {
>> >> >                @Override
>> >> >                public String getIdValue(Category category, int index)
>> {
>> >> >                    return category.getName();
>> >> >                }
>> >> >
>> >> >                @Override
>> >> >                public Object getDisplayValue(Category category) {
>> >> >                    return category.getName();
>> >> >                }
>> >> >            });
>> >> >        }
>> >> >    }
>> >> >
>> >> >
>> >> > Kind regards,
>> >> >
>> >> > Hbiloo
>> >> >
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> >> For additional commands, e-mail: users-help@wicket.apache.org
>> >>
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

Re: DropDownChoice always sets its model to null

Posted by Azzeddine Daddah <wa...@gmail.com>.
The Recipe object is indeed serializable. Below some code from
the AddRecipeForm and Recipe:

public class Recipe implements Serializable {
    public static final int NAME_LENGTH           = 50;
    public static final int DIFFICULTY_LENGTH   = 6;
    public static final int INTRODUCTION_LENGTH = 150;

    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"recipe_seq")
    private Long id;
    ...

    @ManyToOne
    @org.hibernate.annotations.Cascade(CascadeType.SAVE_UPDATE)
    @JoinColumn(name = "CATEGORY_ID")
    @ForeignKey(name = "FK_RECIPE_CATEGORY")
    private Category category;
    ...

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        assert category != null;
        this.category = category;
    }
}

public class AddRecipeForm extends Form<Recipe> {
    private static final Logger log =
LoggerFactory.getLogger(AddRecipeForm.class);

    private final List<FileUpload> uploads = new ArrayList<FileUpload>();
    private Recipe recipe = new Recipe();

    /**
     * Creates a new {@link AddRecipeForm}.
     *
     * @param id form id; may not be <code>null</code>
     */
    public AddRecipeForm(String id) {
        super(id);
        setModel(new Model<Recipe>(recipe));

        add(new FeedbackPanel("feedback"));
        addRecipeInfoFormPart();
        ...
        add(createSubmitLink(this));
    }

    private void addRecipeInfoFormPart() {
        add(new RequiredTextField<String>("recipeTitle", new
PropertyModel<String>(recipe, "name"))
                .add(StringValidator.lengthBetween(5, Recipe.NAME_LENGTH)));
        add(new CategoriesDropDown("categories", new
PropertyModel<Category>(recipe, "category")).setRequired(true));
        add(new RecipeDifficultiesDropDown("difficulties",
recipe).setRequired(true));
        add(new TextArea<String>("introduction", new
PropertyModel<String>(recipe, "introduction")));
    }
   ...
    private StyledSubmitLink createSubmitLink(final Form<Recipe> form) {
        StyledSubmitLink submitLink = new StyledSubmitLink("submitLink") {
            @Override
            public void onSubmit() {
                RecipeService recipeService = RecipeService.getInstance();
                Recipe recipe = form.getModelObject();
                Category recipeCategory =
recipeService.findCategoryByCategoryName(recipe.getCategory().getName());
                Set<Tag> persistentTags = new HashSet<Tag>();
                for (Tag tag : recipe.getTags()) {
                    Tag persistentTag =
recipeService.findTagByTagName(tag.getName());
                    persistentTags.add(persistentTag == null ? tag :
persistentTag);
                }
                log.debug(String.format("from persisted cat: %s and from
form %s", recipeCategory.getName(), recipe.getCategory().getName()));
                Status persistentStatus =
recipeService.findStatusByStatusName("NEW");
                Recipe newRecipe = new Recipe(recipeCategory,
                        recipe.getDifficulty(),
                        recipe.getIntroduction(),
                        recipe.getMethod(),
                        recipe.getName(),
                        new Integer(0),
                        UserUtils.getUser(),
                        recipe.getPreparationTime(),
                        recipe.getRecipeImages(),
                        new
HashSet<RecipeIngredient>(recipe.getRecipeIngredients()),
                        recipe.getServings(),
                        persistentStatus == null ? new Status("NEW", "New")
: persistentStatus,
                        persistentTags);

                uploadImages();

                recipeService.insertNewRecipe(newRecipe);
                setResponsePage(new
SuccessPage(getString("message.success.addrecipe.title"),
                        getString("message.success.addrecipe.body")));
            }
        };
        submitLink.add(new Label("submit", new
ResourceModel("form.addrecipe.add")));
        return submitLink;
    }

    private class CategoriesDropDown extends DropDownChoice<Category> {
        public CategoriesDropDown(String id, IModel<Category> model) {
            super(id, model,
RecipeService.getInstance().getAllPersistentRecipeCategories());
            setChoiceRenderer(new IChoiceRenderer<Category>() {
                @Override
                public String getIdValue(Category category, int index) {
                    return category.getName();
                }

                @Override
                public Object getDisplayValue(Category category) {
                    return category.getName();
                }
            });
        }
    }

    private class RecipeDifficultiesDropDown extends
DropDownChoice<RecipeDifficulty> {
        public RecipeDifficultiesDropDown(String id, Recipe recipe) {
            super(id, new PropertyModel<RecipeDifficulty>(recipe,
"difficulty"), Arrays.asList(RecipeDifficulty.values()),
                    new IChoiceRenderer<RecipeDifficulty>() {
                        @Override
                        public Object getDisplayValue(RecipeDifficulty
recipeDifficulty) {
                            return
recipeDifficulty.getFormattedDifficulty();
                        }

                        @Override
                        public String getIdValue(RecipeDifficulty
recipeDifficulty, int index) {
                            return recipeDifficulty.getName();
                        }
            });
        }
    }
}

On Sat, Feb 28, 2009 at 6:24 PM, Igor Vaynberg <ig...@gmail.com>wrote:

> where is the code that creates your form? and is Recipe serializable?
>
> -igor
>
> On Sat, Feb 28, 2009 at 8:58 AM, Azzeddine Daddah <wa...@gmail.com>
> wrote:
> > Thanks Igor,
> > I've already tried that but still have the same problem.
> > add(new CategoriesDropDown("categories", new
> PropertyModel<Category>(recipe,
> > "category")).setRequired(true));
> > ...
> > private class CategoriesDropDown extends DropDownChoice<Category> {
> >        public CategoriesDropDown(String id, IModel<Category> model) {
> >            super(id, model,
> > RecipeService.getInstance().getAllPersistentRecipeCategories(),
> >                    new IChoiceRenderer<Category>() {
> >                @Override
> >                public String getIdValue(Category category, int index) {
> >                    return category.getName();
> >                }
> >
> >                @Override
> >                public Object getDisplayValue(Category category) {
> >                    return category.getName();
> >                }
> >            });
> >        }
> >    }
> >
> >
> >
> > On Sat, Feb 28, 2009 at 5:54 PM, Igor Vaynberg <igor.vaynberg@gmail.com
> >wrote:
> >
> >> use a property model instead of recipe.getcategories()
> >>
> >> -igor
> >>
> >> On Sat, Feb 28, 2009 at 8:17 AM, Azzeddine Daddah <waarheid08@gmail.com
> >
> >> wrote:
> >> > Hi,
> >> > I'm trying to use a DropDownChoice to display and store the selected
> >> > category in the database. The value selected in the drop down is
> >> correctly
> >> > set, but when I look to the model (Category) of this drop down, it
> >> returns
> >> > always null. Do I do something wrong? Below some code.
> >> >
> >> > public AddRecipeForm(String id) {
> >> >        super(id);
> >> >        setModel(new Model<Recipe>(new Recipe()));
> >> >        ...
> >> >        add(new CategoriesDropDown("categories",
> >> > recipe.getCategory()).setRequired(true));
> >> > }
> >> > ...
> >> > public void onSubmit() {
> >> >    RecipeService recipeService = RecipeService.getInstance();
> >> >    Recipe recipe = form.getModelObject();
> >> >    Category cat = recipe.getCategory(); // This returns always null
> >> > }
> >> >
> >> > ...
> >> >
> >> > private class CategoriesDropDown extends DropDownChoice<Category> {
> >> >        public CategoriesDropDown(String id, Category category) {
> >> >            super(id, new Model<Category>(category),
> >> > RecipeService.getInstance().getAllPersistentRecipeCategories(), new
> >> > IChoiceRenderer<Category>() {
> >> >                @Override
> >> >                public String getIdValue(Category category, int index)
> {
> >> >                    return category.getName();
> >> >                }
> >> >
> >> >                @Override
> >> >                public Object getDisplayValue(Category category) {
> >> >                    return category.getName();
> >> >                }
> >> >            });
> >> >        }
> >> >    }
> >> >
> >> >
> >> > Kind regards,
> >> >
> >> > Hbiloo
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> >> For additional commands, e-mail: users-help@wicket.apache.org
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: DropDownChoice always sets its model to null

Posted by Igor Vaynberg <ig...@gmail.com>.
where is the code that creates your form? and is Recipe serializable?

-igor

On Sat, Feb 28, 2009 at 8:58 AM, Azzeddine Daddah <wa...@gmail.com> wrote:
> Thanks Igor,
> I've already tried that but still have the same problem.
> add(new CategoriesDropDown("categories", new PropertyModel<Category>(recipe,
> "category")).setRequired(true));
> ...
> private class CategoriesDropDown extends DropDownChoice<Category> {
>        public CategoriesDropDown(String id, IModel<Category> model) {
>            super(id, model,
> RecipeService.getInstance().getAllPersistentRecipeCategories(),
>                    new IChoiceRenderer<Category>() {
>                @Override
>                public String getIdValue(Category category, int index) {
>                    return category.getName();
>                }
>
>                @Override
>                public Object getDisplayValue(Category category) {
>                    return category.getName();
>                }
>            });
>        }
>    }
>
>
>
> On Sat, Feb 28, 2009 at 5:54 PM, Igor Vaynberg <ig...@gmail.com>wrote:
>
>> use a property model instead of recipe.getcategories()
>>
>> -igor
>>
>> On Sat, Feb 28, 2009 at 8:17 AM, Azzeddine Daddah <wa...@gmail.com>
>> wrote:
>> > Hi,
>> > I'm trying to use a DropDownChoice to display and store the selected
>> > category in the database. The value selected in the drop down is
>> correctly
>> > set, but when I look to the model (Category) of this drop down, it
>> returns
>> > always null. Do I do something wrong? Below some code.
>> >
>> > public AddRecipeForm(String id) {
>> >        super(id);
>> >        setModel(new Model<Recipe>(new Recipe()));
>> >        ...
>> >        add(new CategoriesDropDown("categories",
>> > recipe.getCategory()).setRequired(true));
>> > }
>> > ...
>> > public void onSubmit() {
>> >    RecipeService recipeService = RecipeService.getInstance();
>> >    Recipe recipe = form.getModelObject();
>> >    Category cat = recipe.getCategory(); // This returns always null
>> > }
>> >
>> > ...
>> >
>> > private class CategoriesDropDown extends DropDownChoice<Category> {
>> >        public CategoriesDropDown(String id, Category category) {
>> >            super(id, new Model<Category>(category),
>> > RecipeService.getInstance().getAllPersistentRecipeCategories(), new
>> > IChoiceRenderer<Category>() {
>> >                @Override
>> >                public String getIdValue(Category category, int index) {
>> >                    return category.getName();
>> >                }
>> >
>> >                @Override
>> >                public Object getDisplayValue(Category category) {
>> >                    return category.getName();
>> >                }
>> >            });
>> >        }
>> >    }
>> >
>> >
>> > Kind regards,
>> >
>> > Hbiloo
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

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


Re: DropDownChoice always sets its model to null

Posted by Azzeddine Daddah <wa...@gmail.com>.
Thanks Igor,
I've already tried that but still have the same problem.
add(new CategoriesDropDown("categories", new PropertyModel<Category>(recipe,
"category")).setRequired(true));
...
private class CategoriesDropDown extends DropDownChoice<Category> {
        public CategoriesDropDown(String id, IModel<Category> model) {
            super(id, model,
RecipeService.getInstance().getAllPersistentRecipeCategories(),
                    new IChoiceRenderer<Category>() {
                @Override
                public String getIdValue(Category category, int index) {
                    return category.getName();
                }

                @Override
                public Object getDisplayValue(Category category) {
                    return category.getName();
                }
            });
        }
    }



On Sat, Feb 28, 2009 at 5:54 PM, Igor Vaynberg <ig...@gmail.com>wrote:

> use a property model instead of recipe.getcategories()
>
> -igor
>
> On Sat, Feb 28, 2009 at 8:17 AM, Azzeddine Daddah <wa...@gmail.com>
> wrote:
> > Hi,
> > I'm trying to use a DropDownChoice to display and store the selected
> > category in the database. The value selected in the drop down is
> correctly
> > set, but when I look to the model (Category) of this drop down, it
> returns
> > always null. Do I do something wrong? Below some code.
> >
> > public AddRecipeForm(String id) {
> >        super(id);
> >        setModel(new Model<Recipe>(new Recipe()));
> >        ...
> >        add(new CategoriesDropDown("categories",
> > recipe.getCategory()).setRequired(true));
> > }
> > ...
> > public void onSubmit() {
> >    RecipeService recipeService = RecipeService.getInstance();
> >    Recipe recipe = form.getModelObject();
> >    Category cat = recipe.getCategory(); // This returns always null
> > }
> >
> > ...
> >
> > private class CategoriesDropDown extends DropDownChoice<Category> {
> >        public CategoriesDropDown(String id, Category category) {
> >            super(id, new Model<Category>(category),
> > RecipeService.getInstance().getAllPersistentRecipeCategories(), new
> > IChoiceRenderer<Category>() {
> >                @Override
> >                public String getIdValue(Category category, int index) {
> >                    return category.getName();
> >                }
> >
> >                @Override
> >                public Object getDisplayValue(Category category) {
> >                    return category.getName();
> >                }
> >            });
> >        }
> >    }
> >
> >
> > Kind regards,
> >
> > Hbiloo
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: DropDownChoice always sets its model to null

Posted by Igor Vaynberg <ig...@gmail.com>.
use a property model instead of recipe.getcategories()

-igor

On Sat, Feb 28, 2009 at 8:17 AM, Azzeddine Daddah <wa...@gmail.com> wrote:
> Hi,
> I'm trying to use a DropDownChoice to display and store the selected
> category in the database. The value selected in the drop down is correctly
> set, but when I look to the model (Category) of this drop down, it returns
> always null. Do I do something wrong? Below some code.
>
> public AddRecipeForm(String id) {
>        super(id);
>        setModel(new Model<Recipe>(new Recipe()));
>        ...
>        add(new CategoriesDropDown("categories",
> recipe.getCategory()).setRequired(true));
> }
> ...
> public void onSubmit() {
>    RecipeService recipeService = RecipeService.getInstance();
>    Recipe recipe = form.getModelObject();
>    Category cat = recipe.getCategory(); // This returns always null
> }
>
> ...
>
> private class CategoriesDropDown extends DropDownChoice<Category> {
>        public CategoriesDropDown(String id, Category category) {
>            super(id, new Model<Category>(category),
> RecipeService.getInstance().getAllPersistentRecipeCategories(), new
> IChoiceRenderer<Category>() {
>                @Override
>                public String getIdValue(Category category, int index) {
>                    return category.getName();
>                }
>
>                @Override
>                public Object getDisplayValue(Category category) {
>                    return category.getName();
>                }
>            });
>        }
>    }
>
>
> Kind regards,
>
> Hbiloo
>

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


Re: DropDownChoice always sets its model to null

Posted by Brill Pappin <br...@pappin.ca>.
I think your issue is with the way you set it up.
It looks to me as if your DropDownChoice is isolated from your data  
model so that when the form submits you getting the property set to  
null, which it is in the input.

could of things to try:
- use a property model for the form model.
- name the dropdown after the field in the data object you actually  
want to populate from input (category in this case)

- Brill


On 28-Feb-09, at 11:17 AM, Azzeddine Daddah wrote:

> Hi,
> I'm trying to use a DropDownChoice to display and store the selected
> category in the database. The value selected in the drop down is  
> correctly
> set, but when I look to the model (Category) of this drop down, it  
> returns
> always null. Do I do something wrong? Below some code.
>
> public AddRecipeForm(String id) {
>        super(id);
>        setModel(new Model<Recipe>(new Recipe()));
>        ...
>        add(new CategoriesDropDown("categories",
> recipe.getCategory()).setRequired(true));
> }
> ...
> public void onSubmit() {
>    RecipeService recipeService = RecipeService.getInstance();
>    Recipe recipe = form.getModelObject();
>    Category cat = recipe.getCategory(); // This returns always null
> }
>
> ...
>
> private class CategoriesDropDown extends DropDownChoice<Category> {
>        public CategoriesDropDown(String id, Category category) {
>            super(id, new Model<Category>(category),
> RecipeService.getInstance().getAllPersistentRecipeCategories(), new
> IChoiceRenderer<Category>() {
>                @Override
>                public String getIdValue(Category category, int  
> index) {
>                    return category.getName();
>                }
>
>                @Override
>                public Object getDisplayValue(Category category) {
>                    return category.getName();
>                }
>            });
>        }
>    }
>
>
> Kind regards,
>
> Hbiloo


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