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?
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
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.