Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.
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 );
}
This support ticket is created vor 7 Jahren, 3 Monaten. 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.
Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.
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 🙂
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/
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!
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 );
}