PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

checking or unchecking checkbox on form render

  1. Ho would I pre populate a checkbox with "checked" or "unchecked" depending on a variable I pass to the function?

    so far I have:

    function populate_checkbox() {
    if(meets criteria) {
    check the box (what goes here???)
    } else {
    don't check the box
    }
    add_filter("gform_field_value_checkbox", "populate_checkbox");
    Posted 8 years ago on Monday July 11, 2011 | Permalink
  2. ??? any ideas ???

    Posted 8 years ago on Monday July 11, 2011 | Permalink
  3. Your filter function needs to return a comma separated list of all choices that you want to be checked. If you want a choice to remain unchecked, simply don't include it in the list.
    In my example below, the items "First" and "Second" will be checked, while all other choices I have in that field will remain unchecked.

    function populate_checkbox(){
       return "First,Second";
    }
    add_filter("gform_field_value_checkbox", "populate_checkbox");
    Posted 8 years ago on Monday July 11, 2011 | Permalink
  4. Ok, that's makes sense. How do I reference each of the checkboxes? By id? Or by the order in which they are generated?

    Posted 8 years ago on Monday July 11, 2011 | Permalink
  5. I am not sure I follow. If you mean access each checkbox choice inside a field, you don't need to do it. Just return the choice text as one of the items in the comma separated list and that specific checkbox will be checked.

    If you are asking how to access each checkbox field, you do that by the parameter name. When you turn on the option in the form settings to "Allow field to be populated dynamically", you give it a parameter name. That name is used in the hook.

    add_filter("gform_field_value_$parameter_name", "populate_checkbox");

    In your example above, the parameter name was "checkbox", so we used "gform_field_value_checkbox" for the filter name:

    add_filter("gform_field_value_checkbox", "populate_checkbox");
    Posted 8 years ago on Tuesday July 12, 2011 | Permalink
  6. works a treat, excellent!

    Posted 8 years ago on Tuesday July 12, 2011 | Permalink

This topic has been resolved and has been closed to new replies.