Skip Navigation

[Resolved] Requiring fields in CRED using JS not working

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
- 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 Bobby339 9 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2687144

Tell us what you are trying to do?
Make 4 custom fields (that are not required in Toolset) be required when being presented on the front end to visitors

Is there any documentation that you are following?
Yes, I found a few threads about using JS, so I copied the code from the toolset support tech and tweaked it. It did not work.

Details:
I have 4 custom fields (all SINGLE LINE fields) named:

transactionpromoline1
transactionpromoline2
transactionpromoline3
transactioncustomername

the Toolset thread I found suggested adding the JS to the JS Editor box, so this is what I tried to do:

jQuery( document ).ready(function() {
jQuery("input[name='wpcf-transactionpromoline1']").prop('required',true);
jQuery("input[name='wpcf-transactionpromoline2']").prop('required',true);
jQuery("input[name='wpcf-transactionpromoline3']").prop('required',true);
jQuery("input[name='wpcf-transactioncustomername']").prop('required',true);
});

but it does not work. What am I doing wrong?

#2687203

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

In stead of using the custom JS, what if you try to use the Forms API hook: "cred_filter_field_before_add_to_form".

Add the following hook to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

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'];
$form_id = 0;
  
    if ( isset($form_html_id) ) {
        $parts = explode( '_', $form_html_id);
        $form_id = (int) $parts[2];
    }
  
    if ( $form_id == 999999 ) {

   $req_fields_slugs = array('transactionpromoline1', 'transactionpromoline2','transactionpromoline3','transactioncustomername');

    if(in_array($field['id'], $req_fields_slugs)){
        $field['data']['validate']['required'] = array (
            'active' => 1,
            'message' => 'This field is required'
        );
    }
     
}
    return $field;
}

Where:
- Replace 999999 with your original form ID

#2687335

That works! Thank you!

To answers your question

"In stead of using the custom JS, what if you try to use the Forms API hook: "cred_filter_field_before_add_to_form"."

is is because using some JS is easier that custom code. Something as normal as requiring a field should not require this level of programming.