Hello,
I am trying to use:
if ($fields['wpcf-order-number']['value'] != 804)
in
add_filter('cred_form_validate', 'cred_wactivation_validation', 10, 2);
function cred_wactivation_validation($field_data, $form_data) {
list($fields, $errors) = $field_data;
//error_log(print_r($fields,true));
if ($form_data['id'] == 810) {
if ($fields['wpcf-order-number']['value'] != 804) {
$errors['wpcf-order-number'] = 'Already been submitted...';
}
}
return array($fields, $errors);
}
And I get "PHP Notice: Undefined index: wpcf-order-number..."
It is because the function runs twice. When it runs first; $fields is empty. When it runs next; $fields is populated with the expected values.
So instead I use:
if (array_key_exists('wpcf-order-number', $form_data) && $fields['wpcf-order-number']['value'] != 804)
To get rid of the PHP Notice, but the question is:
Is it a normal behavior that it runs twice? It is not mentioned in the docs.
Also, if I use array_key_exists() as seen above, can I be certain that the validation check will always perform as expected?
Thanks in advance,
NG
Dear NG,
The PHP notice: Undefined index: wpcf-order-number...
Means your user did not submit the field "wpcf-order-number" in CRED form, so you are right to use function array_key_exists() in your PHP codes.
For the question:
can I be certain that the validation check will always perform as expected?
That depends on yourself, if you need the field "wpcf-order-number" to be a non-required field, your codes is good.
Dear Luo,
Thanks for you reply!
"...if you need the field "wpcf-order-number" to be a non-required field, your codes is good."
Yeah, that is why I'm concerned, because it is set to be required so that I can look for already submitted forms with the same order-number value and also I only want to accept the posted form data if an order with the same order-number already exits.
"Means your user did not submit the field "wpcf-order-number" in CRED form..."
I do not think that this is the exact case, because we are talking about only one a form submission process, during which the hook runs twice for some reason. I suppose it should run only once, but it does it two times instead.
The first time is the problematic one, when $field_data is effectively empty (contains only two empty arrays): Array ( [0] => Array () [1] => Array ()), so there is nothing to check.
But during the same form submission process, the hook runs the second time, the $field_data being properly populated, so this time my validation check works.
(Note that the code above is just a simplified example, of course I do not always compare against 804 in the actual code used, but look for already existing values in the database.)
I just want to figure out why the hook runs twice in the first place, so that I clearly understand what is going on. I do not want to miss any validation, so it is important to know why it work this way.
Your documentation does not mention this behavior, so I'm a bit stuck. Can you please take a second look at it?
regards,
NG
I can duplicate same problem, you are right, the action hook "cred_form_validate" is triggered twice, so I escalated this thread to 2nd supporters, hope there is a detail explain for you.
It should have already been fixed in the latest version of CRED plugin, you can download it here:
https://toolset.com/account/downloads/
Thanks Luo for reporting it. I will update and test it as soon as I have time.
Please update this thread if you need more assistance, thanks