Skip Navigation

[Closed] Help adding conditional to form display

This support ticket is created 2 years, 10 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 2 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#2327003

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.

#2328471

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/

#2328941

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.

#2329217

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.

#2331189

I'm working on setting up a test site.

#2331257

I have marked this thread as "Waiting for feedback" status, please update here if you still need assistance for it.

The topic ‘[Closed] Help adding conditional to form display’ is closed to new replies.