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.

Problem while setting a field as required / not-required using gform_pre_render

  1. remotecontroldesign

    I'm using 'gform_pre_render' to do a lot of stuff on my forms. This is what I'm trying to achieve:

    I have 20 fields (all set as required from back-end). I want to set only the first 5 fields as 'required' when the current user is administrator, and keep all of them required if the current user has some other custom role that I defined. Using the filter, I check the current_user_can('role') and set the 'isRequired' property as 1 or blank according to the role.

    In the front end, when the form is rendered, I can see the asterisks disappeared for the fields I set as not-required through my code ( by setting 'isRequired' as blank ), but when I submit the form with them as empty, they still fail the validation and my form doesn't get submitted!
    Is there any other parameter from the field object I need to disable? Please help me with this.

    Thanks in advance!

    Posted 10 years ago on Saturday September 24, 2011 | Permalink
  2. In addition to the gform_pre_render, you need to take care of validation.

    This was talking about something different, but the process is the same. There are two parts to it: the pre_render and the validation:

    Please see if that helps get you going in the correct direction.

    Posted 10 years ago on Saturday September 24, 2011 | Permalink
  3. remotecontroldesign

    Hi Chris,
    Thanks for the response!
    Yes, it worked !
    Just tell me if this is the right way:
    I made an array with the inputNames of only those fields which are required when the current user is admin. Then in the 'gform_validation' filter, I looped through the fields, checked whether the current user is admin and if the field's inputName is in the above array, just set its 'failed_validation' property to false, and 'is_valid' property to true.
    Is this okay? Do I need to set 'isRequired' to false again, as I'm doing it already in pre_render?
    I just don't want anything to go wrong with the form meta or something, hence being sure!

    - Rutwick

    Posted 10 years ago on Monday September 26, 2011 | Permalink
  4. If it works, I don't think you're hurting anything doing it like this. What has your experience been so far?

    Posted 10 years ago on Tuesday September 27, 2011 | Permalink