Tell us what you are trying to do?
adding conditional to form display
Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/cred-api/
I need to add a conditional to a form, but I'm having trouble implementing it.
I have a parent CPT, and I need the conditional on the child CTP's form.
The parent post has a category that the child posts chooses from a select form element.
Form select:
Category 1
Category 2
Category 3
Special category
I've added a radio button yes/no switch on the parent to control the visibility of the special category.
IF parent switch is YES, show special category.
I assume I can do this with a version of the sample code on the documentation using "cred_filter_field_before_add_to_form":
code:
// make the first_name and last_name fields in a User form required
add_filter('cred_filter_field_before_add_to_form', 'check_special_func', 10, 1);
function check_special_func($field){
//error_log(print_r($field, true));
if(in_array($field['id'], array('first_name', 'last_name'))){
// in some cases $fields['data'] is an empty string, so you'll need to first set it's expected format for PHP 7.1 compatibility
if (!is_array($field['data'])) {
$field['data'] = array();
}
$field['data']['validate']['required'] = array (
'active' => 1,
'message' => 'This field is required'
) ;
}
return $field;
}
... but I need help editing and implementing this code.
Thanks for any help.
Hello,
How do you setup the child post form, if you are following our document to display the child post form link:
https://toolset.com/course-lesson/selecting-parent-posts-when-using-forms-to-create-child-items/#creating-forms-when-a-parent-post-is-preselected
Section "Creating Forms with a Parent Post Preselected"
After user click the child form link, it will pass an URL parameter to the child post form, the URL parameter value is parent post ID, you can use it to get the parent post field value(a radio button yes/no switch on the parent to control the visibility of the special category), and use it as condition of your custom codes.
More help:
https://developer.wordpress.org/reference/functions/get_post_meta/
After user click the child form link, it will pass an URL parameter to the child post form, the URL parameter value is parent post ID, you can use it to get the parent post field value(a radio button yes/no switch on the parent to control the visibility of the special category), and use it as condition of your custom codes.
Yes, I AM using the parent post ID on the URL to access the child post form.
I do not know how to create the conditional for the cred form. I don't know how to write the conditional or how to create a working function.
You can get the URL parameter value by PHP variable $_GET:
hidden link
Then use it in your custom PHP codes as condition.
According to our support policy, we don't provide custom codes support, if you need more assistance for it, please provide a test site with the same problem, I can setup a demo for you.
I'm working on setting up a test site.
I have marked this thread as "Waiting for feedback" status, please update here if you still need assistance for it.