+ Start a Discussion

Dynamic forms : how do i set the name attribute?

I have a booking sheet that is dynamically built using XML. The page is basicaly a form with lots of checkboxes.


How can I set the name attribute of the checkboxes, and then howe do I read in the values in the controller extenstion class.


What I would like is for the page to have something structured liek this


<input type="checkbox" value="1" id="BookingSheetCheck881" name="data[BookingSheet][check][881]">

<input type="checkbox" value="1" id="BookingSheetCheck882" name="data[BookingSheet][check][882]">

<input type="checkbox" value="1" id="BookingSheetCheck883" name="data[BookingSheet][check][883]">


Then somehow loop through the resulting $_POST params which would be structured as a nested array, something like:

$_POST[ 'BookingSheet' ][ 'check' ][ '881' ]

$_POST[ 'BookingSheet' ][ 'check' ][ '882' ]

$_POST[ 'BookingSheet' ][ 'check' ][ '883' ]




I'm confused. Are you using PHP, Apex Code, or some combination of the two? What's your precise use case here? If you're using Apex Code, you do not get to dynamically generate ID values or names. Instead, structure an Apex Class that mimics the layout of the XML file, load the data into the Class, have the user play with the data, then have the Apex Class serialize the data back into XML (or whatever you need). Your question is a bit vague, which probably explains why you didn't get a timely response from the community. We're here to help you, but we need a bit more information.

Mike BatesMike Bates

I'll do my best to explain what I'm trying to achieve.


I have a web application (it's PHP, but that's not important here) that allows users to build booking sheets, the booking sheets are simply sets of categories that contain sets of labelled checkboxes.


I also have an API, the request to the API requires a few ids to specify which booking sheet is required, the response is XML for that booking sheets categories and checkboxes .


In visual force I have a controller that uses url variables to send a request to the API, the XML response is used to build a booking sheet page.


The booking sheet page is a form, each xml category is put on the page as a pageBlock Section:


<apex:pageBlockSection title="{!Category__c.Name}" columns="1">

 and each checkbox in the category should be an <apex:inputCheckbox>. I have created the page so it looks ok, but I can't find a way to to manipulate the name attribute on the <input> tags that are generated. So posting the form isn't going to give me useful results.


I can't build each booking sheet as it's own class in salesforce as there are way too many, and each sheet changes almost weekly. The booking sheet is just a UI component anyway (I guess it's similar in concept to a salesforce page layout) and is just a way of presenting all the relevant tickboxes in an organised way.


I have a tickbox class (lets call it Tickbox__c here, but it's more complex than just a tickbox), and I have imported all the tickboxes into salesforce. So the <input> names on the booking sheet page need to contain Tickbox__c.Id, so I know which tickboxes were selected when the form is posted.



At the moment I'm having to do this for the checkbox inputs


<input type="checkbox" value="{!item.tickbox.Id}" id="ProductMetaSet{!item.tickbox.Id}" name="product_meta_set-{!item.tickbox.Id}}" />

 and am using jquery $.ajax to post the form to the external application, which then uses the salesforce API to send the results back. That's a pretty convoluted way to do it though, so I'd like to get it to post to salesforce, and have an apex controller handle the posted form.



So, just to clarify, if I have this right, the form is created by Visualforce with the help of this XML code, and the form submission goes go your PHP script through some fancy Ajax footwork, which eventually gets around to posting stuff back through the API, and you'd like to avoid all that nasty business. That about sum it up?


You can create callouts using the Http, HttpRequest, and HttpResponse classes. What you'll want to do, probably, is create a dynamic form generator based on your XML data, and then coerce the data once it gets back into the controller. I'd love to draw up some code, but it would take a bit of effort to draw this out again... but I've already done some footwork here (How to create Apex controls dynamically using Apex class) (and other posts like it, just click on my user account) so you'll just need to work out the bits for retrieving the XML, and serving back to your app.


Dynamic forms are daunting, although if you're just keeping it simple, like just checkboxes (or a simple picklist, etc), you can easily do this. The next few releases will sport a feature dubbed "Dynamic Visualforce" that will make it even easier to create dynamic forms through runtime binding. You can't control the names of the input fields (and won't be able to for the foreseeable future), but you can do this using other methods.