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.

Explicitly set default radio choice using hook

  1. Hello,

    Might be right in front of my face, but I haven't found a way to get this working. I'd like to dynamically set the default radio button selection using a hook like gform_pre_render. I've be trying things along the lines of the attached code.

    Essentially, I could use help understanding how to force the form to have a choice (doesn't matter which) selected upon page load. If I can get there, I believe I can take it the rest of the way myself. Any help is much appreciated!

    add_filter( "gform_pre_render_3", "set_default_dynamically" );
    
    function set_default_dynamically($form) {
    
        foreach( $form["fields"] as $field ){
            if( $field["id"] == 4 ){
               $field["choices"][0]["isSelected"] = 1;
            }
        }
        return $form;
    }
    Posted 6 years ago on Thursday January 31, 2013 | Permalink
  2. I ended up using a bit of javascript to make this happen. If there is a way of doing it through a hook when the form loads, I'd still be interested in knowing it. Thanks!

    Posted 6 years ago on Friday February 1, 2013 | Permalink
  3. I tried a couple things with this and made this code work:
    http://pastebin.com/PUvFxxTd

    I looped through each radio button choice. I don't know if that matters or not. I discovered if I had a selection made in the form builder and I was trying to set an earlier option as selected, the later one would always render as selected. So I clear out all the other isSelected choices.

    I tested for the value of the radio button choice before making it selected. Not sure if that will work in your situation.

    I also used "true" instead of "1", but I'm not sure if that mattered.

    This code worked for me. Let me know if anything in there helps you.

    Posted 6 years ago on Saturday February 2, 2013 | Permalink
  4. Thanks! I may prefer your solution over the javascript I am currently using. I'll most likely test it out this afternoon. At a minimum, your example gives me better insight into how to work with the Field Object, so that is great. Thanks again!

    Posted 6 years ago on Monday February 4, 2013 | Permalink
  5. You're welcome. I learned something in the process too.

    Posted 6 years ago on Tuesday February 5, 2013 | Permalink
  6. Just wanted to drop you one last note to let you know that your solution was absolutely perfect for my needs, and I went ahead and used it in favor of the js that I wrote. Feels cleaner to set the correct radio button before the form renders, and the js that I put in place would sometimes have a slight delay before setting the radio button correctly (though the fact that I noticed it was probably more due to the slow server I am building this project on).

    Posted 6 years ago on Tuesday February 5, 2013 | Permalink
  7. Great. Thanks very much for the update. Let us know if you need any more help going forward.

    Posted 6 years ago on Tuesday February 5, 2013 | Permalink