Skip Navigation

[Resolved] Logical problem

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

Problem:
Make custom post fields required ONLY for the frontend User CRED Form when submitting or editing post from frontend.

Solution:
1. Uncheck the required field option from the back-end (if its selected).

2. Please add this code in your theme’s or child theme’s functions.php file. Please note the comments in this code and replace your field-names (slugs), form ID, etc where needed, otherwise code will not work:

add_filter('cred_form_validate','form_field_validation', 2, 10);
function form_field_validation( $field_data, $form_data ) {
    // field data are field values and errors
    list($fields,$errors)=$field_data;
     
    // validate if specific CRED form ID
     if ( $form_data['id'] == 3650 ) {
          
         // set error message per field
        if ( $fields['wpcf-text-field']['value'] == '' ) // Field slug with wpcf "wpcf-text-field"
        $errors['text-field'] = 'Text Field must not be empty'; // Field slug without wpcf "text-field"
         
        if ( $fields['wpcf-text-field2']['value'] == '' )
            $errors['text-field2'] = 'Text Field 2 must not be empty';  
    }
    return array( $fields, $errors );
}

Related ticket: https://toolset.com/forums/topic/required-fields-not-working-in-cred/#post-304667

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.

This topic contains 5 replies, has 2 voices.

Last updated by Noman 4 years, 1 month ago.

Assigned support staff: Noman.

Author
Posts
#560597

hi, i have a little brainbreaker here...

There are products in 5 possible main categories, each one has another function and template.

For one of those categories, called XYZ, we created a CRED form that will generate a product. That CRED form has different required fields for the user, when he submits it.

However, now when we create or edit ANY product in backend, we can't save it since those custom fields from the cred form are telling they're required. They should be required ONLY for the frontend user when submitting or editing from frontend. Not for the admin.

Because 4 out of 5 different kinds of products have nothing to do with those cred form fields. Only the one 'cred' category has.

Solution: 2 possibilities
1- how to make that those fields are required only for frontend user, not backend user?
or
2- how to make that those fields are required only for products in THAT XYZ category?

Perhaps 1 is a touch better option than 2. Ideally would be to have both solutions 🙂

Thanks a lot!!

#560756

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Filip,

Thank you for contacting Toolset support. Second solution would be too complex and I won’t recommend that. First solution can work using custom code like this:
https://toolset.com/forums/topic/required-fields-not-working-in-cred/#post-304667

CRED filter hook used - cred_form_validate:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Please note this is not possible using builtin features of Toolset and you may be required to make more changes to the example code given above. The basic change I can see that you will need to replace field-name in line #11, 12 (replacing validate-checkboxes).

Custom code is needed for this as per support policy (https://toolset.com/toolset-support-policy/) . If you would like you can contact Toolset recommended service providers to further discuss the custom approach. We have some recommended list of service providers here if you would like to take a look: https://toolset.com/consultant/

Thank you

#560769

hi Norman, i'm not sure we're talking about the same thing?
I saw that topic before i contacted support. Afaiu that topic is not related to my issue, all fields that should be required are required. That topic is about required checkboxes mainly.
Thanks!

#560798

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Filip,

validate-checkboxes is basically name or slug of the custom field, that code can work for any type of field. I have tested the given code and modified it so its easier for you to use and understand. I have added 2 fields in the below code, you can add more fields using the if condition in same way:

1. Uncheck the required field option from the back-end (if its selected at moment). See attached screenshot.

2. Please add this code in your theme’s or child theme’s functions.php file. Please note the comments in this code and replace your field-names (slugs), form ID, etc where needed, otherwise code will not work:

add_filter('cred_form_validate','form_field_validation', 2, 10);
function form_field_validation( $field_data, $form_data ) {
    // field data are field values and errors
    list($fields,$errors)=$field_data;
	
    // validate if specific CRED form ID
     if ( $form_data['id'] == 3650 ) {
		 
         // set error message per field
		if ( $fields['wpcf-text-field']['value'] == '' ) // Field slug with wpcf "wpcf-text-field"
		$errors['text-field'] = 'Text Field must not be empty'; // Field slug without wpcf "text-field"
		
		if ( $fields['wpcf-text-field2']['value'] == '' )
			$errors['text-field2'] = 'Text Field 2 must not be empty';	
	}
    return array( $fields, $errors );
}

Thank you

#562495

Hi Norman, letting you know that this worked like a charm! Thank you very much!!

#562501

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Great to hear that its working now 🙂 Thank you for confirmation, have a great day.