Dear Support,
I have a Generic Field (a checkbox) added to a form, just like this "Terms of use checkbox field" in this tutorial:
https://toolset.com/documentation/user-guides/cred-training-course/part-3-adding-generic-fields-conditional-groups-form/
The tutorial reads: "The Terms of use field is always required and the ad will not be submitted until a user checks it."
It is only true if the browser's JavaScript is ON. Without JavaScript, the form can be posted even with the checkbox unchecked. You should point this out in the tutorial and provide an example hook to resolve it.
While I was trying to solve it with a "cred_form_validate" hook (see below), I noticed that my hook is called TWICE while the form is being executed. Is that a normal behavior?
add_action('cred_form_validate_1111', 'site_cred_form_validate_1111', 10, 2);
function site_cred_form_validate_1111($error_fields, $form_data) {
list($fields, $errors) = $error_fields;
error_log(print_r($fields, true)); // -> THIS ONE SHOWS UP IN THE LOG TWICE, HENCE THE HOOK IS CALLED TWICE
if (empty($fields['terms-accepted']['value'])) {
$errors['terms-accepted'] = 'This field is required!';
}
return array($fields,$errors);
}
Please note that I edited the code part of my first post as I mistakenly posted a previous, non-working version of it.
While I was trying to solve it with a "cred_form_validate" hook (see below), I noticed that my hook is called TWICE while the form is being executed. Is that a normal behavior?
It's not normal when JavaScript is active, but I'm not clear about what should be expected without JavaScript. Let me reach out to my 2nd tier support team to see what the expected behavior is when JS is disabled. Please stand by and I will update you when I have some information to share.
Just a quick update to let you know that I'm still waiting to receive some feedback on this question, so I don't have anything substantial to report just yet.
Hi, after some discussion in the team, we have determined that we may be able to optimize the validation code flow to prevent the validation hook from being called multiple times if JavaScript is disabled. The developers have agreed to create a ticket to address this in a future release of CRED. I'll keep you updated here as work progresses, though I do not have a timeline available for you yet.
Hi,
Thanks for the report. Since the issue doesn't seem to have a side effect, I can live with it.
Regards,
G
Alright, let's close out this ticket and I will be able to reply to you here as work progresses. Thanks!
For future reference - an erratum has been published regarding this issue and our developers are investigating a permanent solution:
https://toolset.com/errata/cred_form_validate-api-hook-called-twice-when-javascript-is-disabled/
For all interested parties, the permanent solution for this issue has been released in Forms 2.4.