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.

Disable/remove/modify output of a checkbox based on user_meta

  1. Hey guys,

    I've spent the day with the Developer Docs and made some great progress. I've hit a bit of a wall that I'm hoping someone can help me figure out.

    I have a well documented gist that's based on the gform_field_content filter documentation
    https://gist.github.com/jacobdubail/5113428#file-gistfile1-php

    Basically, what I want to do is match checkbox fields to user_meta fields. If there's a match, I want to disable the field, or at the very least, add a class so I can disable via javascript.

    Any help?

    Many thanks,
    Jacob

    Posted 11 years ago on Friday March 8, 2013 | Permalink
  2. I've cross-posted to StackOverflow: http://stackoverflow.com/questions/15344515/gravity-forms-modify-checkbox-based-on-user-meta-value

    Posted 11 years ago on Monday March 11, 2013 | Permalink
  3. I'll bring this to the attention of the development team.

    Posted 11 years ago on Wednesday March 13, 2013 | Permalink
  4. The gform_field_content is a very complex hook to use because you are basically responsible to create the entire markup for the field, following Gravity Forms patterns. It doesn't look like you need that much control. If all you need is to hide a checkbox field based on some criteria, the best hook to use the gform_pre_render (http://www.gravityhelp.com/documentation/page/Gform_pre_render).
    You can keep most of the logic you have in your code as far as matching the field, and when you find the right field, you can simply set the $field["cssClass"] property to a class that will hide the field (GF has a gform_hidden class that will probably do the trick for you).

    I hope this helps.

    Posted 11 years ago on Wednesday March 13, 2013 | Permalink
  5. Thanks Alex!

    I appreciate the response.

    Because I'm working with 1 checkbox field, I found that cssClass won't work. It hides the entire field, correct? All of the checkboxes in one field? I need to modify/disable specific checkboxes in one specific field. Can I achieve this with the pre_render hook?

    Thanks again for your help!

    Posted 11 years ago on Wednesday March 13, 2013 | Permalink
  6. Oh. Yea, what I told you will hide the entire checkbox field. To hide individual checkbox choices, you will need to use the gform_pre_render to create javascript / jQuery code that will be echoed to the screen and hide each individual choice. For example, if your checkbox field ID is 3, the following jQuery code will hide the first and second checkbox items

    <script type='text/javascript'>
    jQuery(document).ready(function(){
        jQuery('.gchoice_3_1').hide();
        jQuery('.gchoice_3_2').hide();
    });
    </script>

    You will need to look at your checkbox field markup to figure out how to target each individual checkbox item.

    Posted 11 years ago on Thursday March 14, 2013 | Permalink
  7. That'll work! Thanks a lot, Alex. I really appreciate your help.

    -Jacob

    Posted 11 years ago on Thursday March 14, 2013 | Permalink
  8. Hi again,

    EDIT: I'm using this little script to limit the number of boxes that can be checked. http://gravitywiz.com/2012/06/11/limiting-how-many-checkboxes-can-be-checked/ Going to modify it to work withe the pre_render code now. Will update as soon as it's working.

    I'm just about there.

    Here's a bit of code:

    // build the script that will mark the items as complete
    echo "<script>
    			jQuery(document).ready(function() {";
    
    	foreach ( $completed as $key => $val ) {
    		echo "jQuery('.gchoice_1_" . $val . "')
    						.addClass('complete')
    						.find('input:checkbox')
    						.prop('disabled', true); // also tried 'disabled', 'disabled'. adding class works.
    				";
    	}
    
    echo "});
    </script>";

    Interestingly enough, I can add a class to the :checkbox, but setting the disabled property won't work. I can add a class of disabled, and then after the form is rendered, I can update the disabled property via the dev tools console. Does the form rendering strip out the disabled property? That would be weird...

    Posted 11 years ago on Friday March 15, 2013 | Permalink
  9. .gchoice_1_X actually targets the LI that surrounds the checkbox item, that might be the reason it isn't accepting your disabled property. Try using "#choice_1_X" instead.

    Posted 11 years ago on Monday March 18, 2013 | Permalink