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.

Pre-rendered Checkboxes Won't Submit

  1. I'm having a problem with a field of pre-rendered checkboxes that will only submit if the first checkbox is selected. Users should be able to select any of the checkboxes and submit the form, but they are unable to submit said form unless that first checkbox is checked.

    Here is the URL for the form itself: http://midwestrescue.org/adoption-application/

    Here is the code I've used to pre-render the field:

    add_filter('gform_pre_render_2', 'mwr_populate_dogs');
    add_filter('gform_admin_pre_render_2', 'mwr_populate_dogs');
    function mwr_populate_dogs($form){
    
    	$posts = get_posts('cat=4&numberposts=-1&post_status=publish&meta_value=internal');
    
    	$adoptabulls = array();
    
    	foreach($posts as $post){
    		$adoptabulls[] = array('text' => $post->post_title, 'value' => $post->post_title);
    	}
    
    	$adoptabulls[] = array('text' => 'Adoptabull Not Listed', 'value' => 'Adoptabull Not Listed');
    
    	foreach($form['fields'] as &$field){
    		if($field['id'] == 58) {
    			$field['choices'] = $adoptabulls;
    		}
    	}
    
    	return $form;
    }

    Am I missing something?

    Posted 11 years ago on Wednesday September 12, 2012 | Permalink
  2. For the field "Select the Dog(s) You are Interested in Adopting:*" I submitted without checking a box, and got a validation error. When I submitted with the last box checked, I passed that page and was at a confirmation page. I did not have to check the first dog as the one I wanted to adopt. Is that the same as your experience?

    Posted 11 years ago on Friday September 14, 2012 | Permalink
  3. Anonymous
    Unregistered

    This happens because checkboxes are treated as multi-input fields. Please see this thread:
    http://www.gravityhelp.com/forums/topic/dynamic-checkboxes

    Posted 11 years ago on Friday September 14, 2012 | Permalink
  4. Thank you for finding that mobt. I tried yesterday and could not remember the phrase "surprisingly complex." It's good to re-read that one.

    Posted 11 years ago on Friday September 14, 2012 | Permalink
  5. Anonymous
    Unregistered

    Hi Chris,

    I have found out that checkbox items added dynamically on "gform_pre_render" / "gform_admin_pre_render" are not available to functions added to form submission hooks (such as "gform_validation", "gform_pre_submission" etc), unless the form is saved in the GF admin area after dynamic population.

    When pure arbitrary data is used to pre-render the checkboxes, the above workaround is not possible.

    Therefore I use the following approach which seems to work very well for me: on "gform_pre_render" I use RGFormsModel::get_form_meta() method to fetch the form from database, then populate the form controls as required, and then the form is saved back to the database using RGFormsModel::update_form_meta(). This behaviour can be controlled using a boolean constant, as shown below:

    // When true, the form will be saved to DB after dynamic population
    define("SAVE_FORM_ON_PRE_RENDER", true);
    
    // Adds a filter to form id 26. Replace 26 with your actual form id
    add_filter("gform_pre_render_26", populate_checkbox);
    add_filter("gform_admin_pre_render_26", populate_checkbox);
    
    function populate_checkbox($form) {
    
        if (SAVE_FORM_ON_PRE_RENDER)
          $the_form = RGFormsModel::get_form_meta($form['id']);
        else
          $the_form = &$form;
    
        // Creating choices
        $choices = array(
          array("text" => "Test 1", "value" => "test1"),
          array("text" => "Test 2", "value" => "test2"),
          array("text" => "Test 3", "value" => "test3"),
        );
    
        $inputs = array(
          array("label" => "Test 1", "id" => "2.1"), //replace 2 in 2.1 with your field id
          array("label" => "Test 2", "id" => "2.2"), //replace 2 in 2.2 with your field id
          array("label" => "Test 2", "id" => "2.3"), //replace 2 in 2.3 with your field id
        );
    
        // Adding items to field id 2. Replace 2 with your actual field id.
        // You can get the field id by looking at the input name in the markup.
        foreach ($the_form["fields"] as &$field) {
            // replace 2 with your checkbox field id
            if ($field["id"] == 2) {
                $field["choices"] = $choices;
                $field["inputs"] = $inputs;
            }
        }
    
        // Save form to database, if constant is set to true
        if (SAVE_FORM_ON_PRE_RENDER)
          RGFormsModel::update_form_meta($form['id'], $the_form);
    
      return $the_form;
    }

    Using this approach you can view the checkbox values in the entry list as well as change the entry, by updating the submitted checkbox values.

    Hope this helps.

    Posted 11 years ago on Monday September 17, 2012 | Permalink
  6. Thank you for posting that as well. I'm certain that will be helpful to someone.

    Posted 11 years ago on Tuesday September 18, 2012 | Permalink