You shouldn't have to add any checkbox items to your checkbox field in the form editor in order to dynamically populate it using the code example you are using. It replaces the values entirely, the existing values don't act as placeholders.
I just tested the example you are working from and implemented it on my own test site and it works fine. The example I am referring to is here:
I implemented this code, exactly as is, on my test site and had no problem using it to dynamically populate the checkbox field. I am including screenshots of what I have done below.
I created a new form and on that form I placed a Checkbox field that I want to dynamically populate. The field consists of only 1 choice, but it doesn't matter as all the choices will be replaced by my custom code. See this screenshot:
After creating the form with the checkbox I then implemented the example code to populate that field dynamically using the gform_pre_render hook by placing custom code in my themes functions.php file. See this screenshot:
Now when I preview my form, the checkbox field is dynamically populated with the items it's being populated with in the custom code that I implemented as opposed to the choices in the form editor itself. See this screenshot:
So the example code does indeed work. If it's not working in your implementation that means that it wasn't implemented properly or the custom code related to getting and returning the values you want to populate the field is not functioning correctly.
I would start again from the beginning and implement the code exactly as it is found here:
Then update it to suit your needs. The issue is happening on your end with how you are populating the array of choices and inputs. It sounds like if it's only passing the first value, that you aren't actually passing an array of values like you should be.
Posted 10 years ago on Monday April 30, 2012 | Permalink