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);
}
}
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
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?
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.
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.
Do you need complete backup of this website?
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.
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?
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.
Hi, I have removed the personal data from the site so now you can take backup.
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.
Thanks Waqar!
I will check this and update you if this solves the issue.
You're welcome and glad I could help.