Skip Navigation

[Resolved] How to make all custom fields required

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by philipC-4 3 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#2178721

Tell us what you are trying to do?

How do you make all fields “required”?

We have a form named Structural Systems that a user can use to inspect their home, we would like for all custom fields that don’t get selected or “inspected” to be required so that they are forced to pick one. This way something gets printed out when you see your inspection results after submitting.

Inspection form:
hidden link

What is the link to your site?
hidden link

You should have backend login credentials on file, please let me know if I need to provide them again.

#2178899

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

Unfortunately i'm not able to see the form on the page.

However if you want to make the custom fields required then you can do it by going to Toolset -> Custom Fields and edit the field group that the custom fields belong to.

Once you've done that you can edit each of the custom fields and ensure that the required options are checked.

For those fields that cant be set as required you can use the hook below to create a custom validation function to force the field to be required.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Please let me know if this helps.
Thanks,
Shane

#2178947

Hello Shane,

You will need login credentials to be able to view the form. Do you still have access to the backend? Christian was the former Support specialist that would help us out. Not sure if that will help?

#2181029

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

I may not need the credentials at this stage as my instructions in the previous response should be able to assist you with getting your fields to be required.

If there are any issues with this please let me know.

Thanks,
Shane

#2183177
Screen Shot 2021-09-29 at 2.36.08 PM.png

Hello Shane,

We have looked into this and we still need your help. We are using the "checkboxes" custom field and we need the user to be forced to pick "inspected" or "not inspected" on all items. (Screenshot attached)

If at all possible, please log in and take a closer look. Your help is greatly appreciated.

#2183233

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

Yes please provide me with admin access to the site and a link to the form page.

For this field it will require our validation hook to make it required.

I've enabled the private field for your next response.

Thanks,
Shane

#2183817

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Philip,

Thankyou for the credentials.

I've added the following code to Toolset -> Settings -> Custom Code.

// Put the code of your snippet below this comment.
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==331)
    {
      
        //check if the user selected a Grading and Drainage value
        if (empty($fields['wpcf-grading-and-drainage']['value']))
        {
            //set error message for Grading and Drainage 
            $errors['grading-and-drainage'] = 'No value selected';
        }
    }
    //return result
    return array($fields,$errors);
}

You form should now be checking if the user selects the field option.

Thanks,
Shane

#2186941

My issue is resolved now. Thank you!