Skip Navigation

[Resolved] How can we make checkboxes 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 13 replies, has 2 voices.

Last updated by matthiasV-2 3 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1893813

Tell us what you are trying to do? I would like to make checkboxes fields required, how can I do that?

#1893913

Shane
Supporter

Languages: English (English )

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

Hi Matthias,

Thank you for getting in touch.

In order to best help with this one I would need to know if this is for the backend or is it for a Frontend form?

The reason is because there isn't a validation function for the fields on the backend and there isn't any provision in our UI to make this field required.

However on the frontend form we can use a validation hook to check to ensure that the checkbox isn't submitted empty.

Please let me know and we can take it from there.

Thanks,
Shane

#1893925

A backend form that we set once and that users can fill out and send.

#1893937

Here's the page: hidden link

#1893939

Shane
Supporter

Languages: English (English )

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

Hi Matthias,

A backend form that we set once and that users can fill out and send.

I'm assuming here that you are referring to a frontend form.

Unfortunately the link to the page that you've sent requires that i'm logged in.

Can you allow me to have temporary admin access so that I can have a look at this for you.

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

Thanks,
Shane

#1894267

Shane
Supporter

Languages: English (English )

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

Hi Matthias,

Can you try this code below.

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']==710)
    {
     
        //check if featured image exists
        if (empty($fields['type-klant']['value']))
        {
            //set error message for featured image
            $errors['type-klant'] = 'No value selected';
        }
    }
    //return result
    return array($fields,$errors);
}
 

Can you try this and let me know if it helps. You can add it to your theme's functions.php file or our Toolset custom code section and activate it.

Thanks,
Shane

#1894269

I tried to add the above code over Toolset's custom code feature in settings but unfortunately the snippet cannot be edited directly. I get a warning to check file permissions and wp-config.php constants DISALLOW_FILE_EDIT and DISALLOW_FILE_MODS. Ot ot alternatively edit the file manually.

Can you add the code for us please?

#1894271

Shane
Supporter

Languages: English (English )

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

Hi Matthias,

In this case you will need to edit the functions.php file by logging into your site using FTP.

If you are not sure how to do this then please let me know as well as provide me with the FTP login so that I can check on this for you.

Thanks,
Shane

#1894299

Shane
Supporter

Languages: English (English )

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

Hi Matthias,

This should now be working.

You can go ahead and check the form and you will see that it won't submit unless the checkbox value is selected.

Thanks,
Shane

#1894337

You added the code in expert mode.
By switching back to the visual editor all the edits to the form are lost.
Moreover only the first checkbox is required to fill out now, though we want to choose which checkboxes should be mandatory.
This is not what we asked for.
We asked for a simple fix to make checkboxes of our choice mandatory or not in the visual editor.
Reset the form as it was before and get the fix done.

#1894343

Shane
Supporter

Languages: English (English )

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

Hi Matthias,

The only way to make the fields required is through the validation hook.

With your form the main issue is that we are unable to set all of them as required because most of them are hidden in conditionals. SO the hook will check all the fields to see if they are selected.

Unless all the fields have a value selected then the form won't submit. So the solution to this is to make only the dependent checkbox required, which is used to trigger the other checkbox fields.

Please let me know if this is clear.

Thanks,
Shane

#1894351

Reset the form as it was in the visual editor (without the checkbox being required).
We'll drop this feature.

#1894383

Shane
Supporter

Languages: English (English )

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

Hi Matthias,

The form is displaying as intended on the frontend. However i'm seeing where the generic fields are missing.

Are you able to recreate the conditionals in the visual builder?

When you go to the page below please let me know what you are seeing.
hidden link

Also if you have a backup of the site you can restore this if the form elements are missing. Also you mention this "You added the code in expert mode."

I didn't add any code in expert mode. The code I added was added to Toolset->Settings->Custom Code.

The form was switched to expert mode to see what the field names were in order to setup the custom validation.

Thanks,
Shane

#1894403

My issue is not resolved but I don't need anymore help on this issue.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.