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.

Remove fields dynamicaly using "gform_pre_render" filter

  1. Hello, here is my problem. I have a form, and when showing the form on page, I need to hide some of the fields. I am using the "gform_pre_render" filter for this, as I found a lot of reference that it should be used for this. It works at the first sight and the fields are really hidden, but it seems that it only filters fields when the form is displayed, but when I submit the form and it goes through validation, it is validated according to old fields settings instead of the filtered ones. It is no matter if I just unset the fields from form or I set their "isRequired" value to false, always the forms displays correctly, but the validation fails, because required fields are just missing.

    Here is my code I am using:

    function ins_remove_fields($form) {
    	$fields_to_hide = array('test');
    	// remove fields to hide
        foreach($form['fields'] as $key=>$field) {
            if(in_array($field['label'], $fields_to_hide)) {
    			//$form['fields'][$key]['isRequired'] = false;
        return $form;
    add_filter('gform_pre_render', 'ins_remove_fields');

    while "test" is the name of one of the required fields.

    Do you have any idea what I might be doing wrong ? I think it should be correct like this, but apparently it is not.

    Thank you in advance

    Posted 10 years ago on Thursday September 1, 2011 | Permalink
  2. Can you uncheck the "required" setting in the form builder to make them not required? If they're not required, they won't be validated.

    Something Alex posted on a related topic a few days ago:

    Posted 10 years ago on Saturday September 3, 2011 | Permalink
  3. Hi Chris, I know it works if the fields are not required, but I need them required. I mean, either they are in form and are required, or they are hidden and of course not required. How can I achieve this using gform_pre_render filter ?

    I was looking at Alex's posts, but that doesn't help me much, it is only about automatically populating fields option. And its state doesn't do any difference in my case.

    What I am looking for at the best case is some filter, that would allow me remove fields from validation when submitting form. Or it would be nice, if the on-submit validation would also go through the gform_pre_render filter, so the fields are really handled the same way they are rendered.

    So in short - I remove some fields using gform_pre_render and they are still validated as if they are there. How can I turn off this validation dynamically ?

    Thanks a lot

    Posted 10 years ago on Wednesday September 7, 2011 | Permalink