Skip Navigation

[Resolved] Undefined index of array when using cred_form_validate

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

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by Luo Yang 7 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#436487

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

#436739

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.

#437566

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

#437837

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.

#469182

It should have already been fixed in the latest version of CRED plugin, you can download it here:
https://toolset.com/account/downloads/

#469332

Thanks Luo for reporting it. I will update and test it as soon as I have time.

#469676

Please update this thread if you need more assistance, thanks

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.