Skip Navigation

[Resolved] Dynamically displaying to a specific form field required on toolset form

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

Problem:
Dynamically displaying to a specific form field required on toolset form

Solution:
You can use "cred_filter_field_before_add_to_form" toolset form hook using which you can set few fields dynamically required.

You can find proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/dynamically-displaying-to-a-specific-form/#post-2210489

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_filter_field_before_add_to_form

This support ticket is created 3 years 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.

Our next available supporter will start replying to tickets in about 0.97 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Saul Baizman 3 years ago.

Assisted by: Minesh.

Author
Posts
#2210267

Hi there,

I'm using a CRED form to let website visitors submit a form. We have two versions of the form: a public form (for the public) and a private form (for internal stakeholders, and password-protected). They share most of the same fields but have a couple of differences. I'm using the cred_filter_field_before_add_to_form hook to dynamically make a few fields required. Can I make certain fields on the public form required and certain other fields on the private form required? If so, how?

Thank you!

Saul

#2210489

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As shared with the previous ticket there are two methods you can use "cred_filter_field_before_add_to_form " using which you can set few fields dynamically required.

So to identify the form ID we can use the following method within the "cred_filter_field_before_add_to_form" hook.

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 1);
function required_fields_func($field){
    
   
        $form_html_id = $field['form_html_id']; 
      
      if ( isset($form_html_id) ) {
        $parts = explode( '_', $form_html_id);
        $form_id = (int) $parts[2];
    }

      if($orm_id == 9999){
                               if(in_array($field['id'], array('new-single-line'))){
                                                    $required = array('required' => array(
                                                                                        'active' => 1,
                                                                                           'value' => true,
                                                                                               'message' => "This field is required."
                                                                                        	));
                                                  $field['data']['validate'] =  $required;
                               }

      }else   if($orm_id == 1111){
                            if(in_array($field['id'], array('another-field-slug'))){
                                                    $required = array('required' => array(
                                                                                        'active' => 1,
                                                                                           'value' => true,
                                                                                               'message' => "This field is required."
                                                                                        	));
                                                  $field['data']['validate'] =  $required;
                               }

      }
      
    return $field;
}

I hope this helps to resolve your issue.

#2211261

This is exactly what I was looking for. Thank, Minesh!