Skip Navigation

[Resolved] Required fields in front-end but not Back-end form

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to make a checkbox required in a Form, but not in wp-admin.

Solution: Use the Forms API to make the checkbox required in Forms:

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 1);
function required_fields_func($field){
    if(in_array($field['id'], array('gdpr-acceptance'))){
        $field['data']['validate']['required'] = array (
            'active' => 1,
            'message' => 'This field is required'
        );
    }
    return $field;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_filter_field_before_add_to_form

100% of people find this useful.

This support ticket is created 5 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by patrikK-2 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1229318

I have a setup that works very good. but is there any way to have the form fields to be Required ONLY in front end and NOT in Admin (back end)

#1229343

You can use the Forms API to do this a couple of different ways. The first option can enforce the required field before the Form is submitted. The second option will enforce the required field during form submission:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_filter_field_before_add_to_form
Here's an example showing how to make the first_name and last_name fields required:

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 1);
function required_fields_func($field){
    if(in_array($field['id'], array('first_name', 'last_name'))){
        $field['data']['validate']['required'] = array (
            'active' => 1,
            'message' => 'This field is required'
        );
    }
    return $field;
}

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
Here's an example showing how to add your own custom validation to a Form:

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']==12)
    {
        //check my_field value
        if ($fields['wpcf-my_field']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['wpcf-my_field']='Wrong Value';
        }
        //check if featured image exists
        if (empty($fields['_featured_image']['value']))
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Missing featured image';
        }
    }
    //return result
    return array($fields,$errors);
}
#1229358
filed.JPG
gdpr_field.JPG

Ok so if i understand you correct. My field i have (Checkbox accept gdpr ) i can remove the Requierd from the field and instead add the code in functions.pho for just that field ?
So it will just be requierd in front-end for the user, but not in admin where i can edit the post without need to accept the checkbox ?

#1229370

Yes, that is correct. The checkbox will be required on the front-end Form, but not in wp-admin.

#1229388

ok, så this code would work with checkbox ?

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 1);
function required_fields_func($field){
    if(in_array($field['id'], array('gdpr-acceptance'))){
        $field['data']['validate']['required'] = array (
            'active' => 1,
            'message' => 'This field is required'
        );
    }
    return $field;
}

IT DID 😀

#1229414

Looks good to me!

#1229416

Yes it did to me too 🙂
And worked !
thank you so much!! have a nice weekend!

#1229418

My issue is resolved now. Thank you!