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.

Argh - gform_pre_render again

  1. dochara
    Member

    You may want to merge this into my previous thread, as I thought I'd solved it, but haven't :(

    Problem arises when unsetting a required field - it's still required. So, I need something extra here, but not sure what.

    function sd_hide_or_require_insured($form){
    	$insuredfield = array('Insured');
    	global $post;
    	$needsinsurance = get_post_meta($post->ID,'_sd_prod_insurance',true);
    	if ($needsinsurance==1) {
    		//make the field required  - but how?
    	} else {
    		foreach($form['fields'] as $key=>$field) {
    			if(in_array($field['label'], $insuredfield)) {
    				unset($form['fields'][$key]);
    //or alternatively make it not required if value is 0,  as well as unsetting
    			}
    		}
    	}
        return $form;
    }

    Any help really appreciated.

    Posted 8 years ago on Monday January 16, 2012 | Permalink
  2. dochara
    Member

    I figured it out. It requires a two step process:

    1. Unset the field using gform_pre_render if it is not needed
    2. Validate it using if it is needed

    I've posted my code here for anyone who is interested.

    http://pastebin.com/pQ4TCTzh

    Posted 8 years ago on Monday January 16, 2012 | Permalink