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)
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);
}
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 ?
Yes, that is correct. The checkbox will be required on the front-end Form, but not in wp-admin.
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 😀
Yes it did to me too 🙂
And worked !
thank you so much!! have a nice weekend!
My issue is resolved now. Thank you!