Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSF and the fly likes dynamic JSF forms, ajax, validation, etc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "dynamic JSF forms, ajax, validation, etc" Watch "dynamic JSF forms, ajax, validation, etc" New topic
Author

dynamic JSF forms, ajax, validation, etc

Dudley Dawson
Ranch Hand

Joined: Dec 14, 2004
Posts: 57
Hi-
I have a problem creating a dynamic form in jsf. I will try to explain the issue here while being as generic as possible. Apologizing in advance for the length of the question, but I am running into many walls and time is running out on my project.
Say there is an application in which a user can select which kind of food they want at a certain number of upcoming meals. The number of upcoming meals is determined by the application. Each meal will get its own 'row' in the form, which will contain 1) a dropdown to select a food type, 2) another dropdown to select some modifier of that food type, 3) a text input to specify the quantity of that food.
so, I have a datatable who's "var" attribute is a collection of beans that hold the values of the three datafields for each meal:

there is a method getMeals() in myForm which will return a collection of Meal beans, each containing a data member for food, modifier, and quantity, based on the user's context. This works. Also, the food dropdown is populated by the getAvailableFoods() method, also based on user context. This also works. Now for my problem(s):
I'm finding it very hard to dynamically reference the various form components. For instance, I have attached ajax support the the food dropdown. what I would like to happen is, when a user selects a food from the dropdown, the modifiers are dynamically populated in the next dropdown dynamically. For instance, if the user selects 'sandwich' for food, the next dropdown would be populated with 'turkey', roast beef', 'pastrami', etc. If they select 'Fruit' on the first dropdown, the next dropdown gets 'apple', 'pear', 'orange', etc. The problem is-
In my target ajax method, how can I access the correct modifier component to add selectItems to? I tried creating collections of components that are part of the meal bean itself, and bind them to the JSF components, but i got the error:
Target Unreachable, identifier 'meal' resolved to null.
I don't know how to access dynamically generated components programmatically.
I also have a similar problem- the validation rules for 'quantity' will change depending on which 'modifier' was selected. How can I dynamically change the validation criteria for that textfield. I have found the methods to do it in the api, but my problem is, how do i get access to the correct 'quantity' textfield that corresponds to the 'modifier' that has changed???

Thank you
[ August 21, 2007: Message edited by: Dudley Dawson ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Sorry that I won't be much help here, but aren't there third party components that already do this, and aren't there also examples on the web on how to incorporate this.

I vaguely remember Ajax in Action even having code for this.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Dudley Dawson
Ranch Hand

Joined: Dec 14, 2004
Posts: 57
update-
I've abandoned my strategy posted here. I'm able to achieve exactly want programmatically by using the api. It's kind of sloppy, but it works
 
jQuery in Action, 2nd edition
 
subject: dynamic JSF forms, ajax, validation, etc