Skip Navigation

[Resolved] My custom form validation is not working but previously it was working.

This support ticket is created 2 years, 3 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/Karachi (GMT+05:00)

This topic contains 12 replies, has 2 voices.

Last updated by Waqar 2 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2254751

Hi Team,
My custom form validation is not working but previously it was working for another form. I have used the same custom validation script and just changed the function name and form id.
Could you please help me?

// function for validation on Registration form
add_filter('cred_form_validate','nhs_form_validation',10,2);
function nhs_form_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;
if ($form_data['id']==3053)
{

$user_email = $fields['user_email']['value'];
$user_email_domain = explode( '@', $user_email );
$user_email_domain = $user_email_domain[1];

//check my_field value
if( $user_email == 'specific-domain.com' ) {
$errors['user_email'] = 'If you are a specific-domain employee, you do not need to register. Just click on the blue button above to access Insight.';

}
//return result
return array($fields,$errors);
}
}

#2255141

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

The code that you shared seems correct, except for this line:


if( $user_email == 'specific-domain.com' ) {

Please change it to use "$user_email_domain" for the evaluation and it will work:


if( $user_email_domain == 'specific-domain.com' ) {

regards,
Waqar

#2255251

Hi Waqar,
I have changed that variable name, by mistake I have put another one inside IF condition but the validation is still not working and form is still submitting without that validation. What should I do next?

#2255277

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Can you please share temporary admin login details, along with the link to the form page and location of this custom code?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2255923

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

The exact code snippet is working on my test website, but, on your website, I'm seeing unexpected behavior.

Do I have your permission to download a clone/snapshot of your website? This will help in troubleshooting this in more detail on my test server.

#2255941

Do you need complete backup of this website?

#2256035

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes, I'll need the complete backup of the website (i.e. files and the database), to deploy it on a test server.

But in case it is a large website, I'll exclude the "uploads" folder, which includes the media library files.

#2257237

okay, let me take backup and delete some personal data from the site and then you can copy the site. Please let me know, will you take backup/copy of site by urself or should I give you backup file?

#2257325

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Since I have the admin access details, it would be more efficient, If you let me download the clone/snapshot of the website, directly.

I'll wait for the go-ahead to download it from your side.

#2258343

Hi, I have removed the personal data from the site so now you can take backup.

#2259199

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for the permission.

During troubleshooting on your website's clone, I noticed that issue with the validation function was resulting from a different snippet named "Honeypot for registration form".

In the function used in that code snippet, the line "return array($fields,$errors);" is placed inside the "if" condition that checks for the specific form:


add_filter('cred_form_validate','honeypot_function',10,2);
function honeypot_function($field_data, $form_data)
{
	list($fields,$errors)=$field_data;

	if ($form_data['id']=='1349') {
		//honeypot field
		$honeypot = $_POST['insight-honeypot'];
		//check if the honeypot field is filled out.
		if ($fields['insight-honeypot']['value']!='')
		{
			//set error message for field
			$errors['insight-honeypot']='Wrong Value';
		}

		//return result
		return array($fields,$errors);
	}
}

Whereas that line should be outside the if condition, like this:


add_filter('cred_form_validate','honeypot_function',10,2);
function honeypot_function($field_data, $form_data)
{
	list($fields,$errors)=$field_data;

	if ($form_data['id']=='1349') {
		//honeypot field
		$honeypot = $_POST['insight-honeypot'];
		//check if the honeypot field is filled out.
		if ($fields['insight-honeypot']['value']!='')
		{
			//set error message for field
			$errors['insight-honeypot']='Wrong Value';
		}
	}
	
	//return result
	return array($fields,$errors);
}

Fixing this code snippet will fix the behavior of the validation function used in the other code snippet "NHS Registration Form Validation" too.

#2259271

Thanks Waqar!
I will check this and update you if this solves the issue.

#2259947

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You're welcome and glad I could help.

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