frm_validate_entry error handling not working

By: Roger Weiss | Asked: 10/26/2022
ForumsCategory: Code Snippetsfrm_validate_entry error handling not working
Roger WeissRoger Weiss asked 2 years ago
I'm using snippets with formidable forms, see relevant code below, which should trigger an error  (and used to!) I have verified that  "$errors['deadline']" IS populated (see my commented out echo statement) But, the error handling is not kicking in. Any ideas out there?  I already emailed Formidable support and they sent me here. Thanks for your help, Roger =============================================== add_filter('frm_validate_entry', 'validate_my_form', 20, 2);
function validate_my_form($errors, $values){ if ($values['form_id'] == 7){ . . . if ($deadline == "Y") {
$errors['deadline'] = '<strong style="font-size:1.1em;">The deadline for this application has passed</strong>';
}

} // end form id = 7
// echo "Error: " . $errors['deadline'];
return $errors;
}
7 Answers
Bobby Clapp answered 2 years ago
Where is the variable that defines $deadline to be able to check for "Y"?
Roger WeissRoger Weiss replied 2 years ago

I set that in previously called php code. I know that it does get set since I displayed the contents of $errors['deadline'], and it had been filled in with the msg I set.
Note: I left out most of the body of code to simplify your review.

RW

Rob LeVineRob LeVine replied 2 years ago

I had the same question as Bobby and then figured the "..." meant you left code out. I suggest pasting the full code somewhere such as paste2.org and posting the link.

Roger WeissRoger Weiss answered 2 years ago

OK, If you want to see the code, here it comes. Not sure how much it will help, tho...  

Start your code here:

add_filter('frm_validate_entry', 'validate_my_form', 20, 2); function validate_my_form($errors, $values){ if ($values['form_id'] == 7){ $email = wp_eMember_get_user_details("email"); include "/home/customer/www/nepta.com/public_html/mysql_connect.php"; $recital = $_POST['item_meta']['448']; // if today is between Jan 1 and before March 2, renew date must be lastyear-0101 thru lastyear-09-30 // If today is betweeen Oct 15 and Dec 31,t then renew date must be thisyear-0101 thru this year-09-30 $today = date("Ymd"); $thisyear = date("Y"); $lastyear = $thisyear - 1; $JanDate = $lastyear . "-01-00"; $OctDate = $lastyear . "-10-01"; $JanDate2 = $thisyear . "-01-00"; $OctDate2 = $thisyear . "-10-01"; // Set and check deadlines $dlj1 = "20221211"; $dlj2 = "20221225"; $dlint1 = "20230101"; $dlint2 = "20230108"; $dlcomjz20 = "20230115"; $dlens = "20230305"; $dlsen = "20230208"; $dlfrei = "20230319"; $dlham = "20230326"; $dladult = "20230305"; $dlhalloween = "20221006"; $dlmushope = "20221023"; //Get date parms here $today1 = date("md"); $roger = "About to call editdata"; include "/home/customer/www/nepta.com/public_html/members/editData.php"; // Set and Check Oversubscription include "/home/customer/www/nepta.com/public_html/members/teacherchk.php"; // Return Errors if ($deadline == "Y") { $errors['deadline'] = '<strong style="font-size:1.1em;">The deadline for this application has passed</strong>'; } if ($overlimit == "Y") { $errors['overlimit'] = '<strong style="font-size:1.1em;">You have already reached your student limit</strong>'; } } // end form id = 7 // echo "Error: " . $errors['deadline']; return $errors; }
Bobby Clapp replied 2 years ago

I still don't see $deadline being declared with a default value prior to the check of if ($deadline == "Y") {

Roger WeissRoger Weiss answered 2 years ago
That value is being set in editData.php. Please note, I have already verified that $deadline is being set to "Y", and that $errors['deadline'] is indeed being populated. Note the commented out echo line. When activated, it showed the error msg. So, I know all my code is working. Maybe its a plugin conflict???   RW
Bobby Clapp replied 2 years ago

I have to ask the boring questions, right?!? 🙂

Is "validate_my_form" being declared for any other function?

Rob LeVineRob LeVine replied 2 years ago

At least for me, this is going to be really hard to debug "at a distance". If you want to pay someone to debug, I would hope someone could do that. Otherwise, the only things I can think of are
1. Manually set $errors to something like $errors['xxx] = 'XXX' to eliminate the possibility that the HTML is messing something up.
2. Verify you have no other frm_validate_entry that's being run and overriding it.
3. Check your form settings for something odd
4. Check the java console for an error
5. Turn error checking on and check your WP log for errors.

Roger WeissRoger Weiss answered 2 years ago

Thank you for all your input. So, I deactivated the 2 snippets that were using the same function (frm_validate_entry) and it worked fine. So, now that we've narrowed down the problem, how do I activate these 2 snippets and be sure they all work? I have already shared the snippet in question. I will display the other 2 below and perhaps one of you can see my error. 

add_filter('frm_validate_entry', 'validate_username6', 20, 2);
function validate_username6($errors, $values){
if ($values['form_id']  == 6){
	$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
//Check connection
if (mysqli_connect_errno()) {
    echo 'Could not connect to MySQL: ' . mysqli_connect_error() ;
}
mysqli_select_db ($dbc, DB_NAME );
$memberdb = "wphq_wp_eMember_members_tbl";
	
	$user = $_POST['item_meta']['765'];
	$email = $_POST['item_meta']['173'];
	
	$check = mysqli_query($dbc,"SELECT * FROM $memberdb WHERE email = '$email'");
	$rowcount = mysqli_num_rows($check);
	if ($rowcount != 0) {// email has been found - error
		 $errors['my_error'] = 'This email already exists.';
	     return $errors;
		 }
	$check = mysqli_query($dbc,"SELECT * FROM $memberdb WHERE user_name = '$user'");
	$rowcount = mysqli_num_rows($check);
	if ($rowcount != 0) {// user name has been found - error
		 $errors['my_error'] = 'This user name already exists.';
	     return $errors;
		 }
  }

}
/* Validate email address exists  */
add_filter('frm_validate_entry', 'validate_email27', 20, 2);
function validate_email27($errors, $values){
if ($values['form_id']  == 27){
	$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
//Check connection
if (mysqli_connect_errno()) {
    echo 'Could not connect to MySQL: ' . mysqli_connect_error() ;
}
mysqli_select_db ($dbc, DB_NAME );
$memberdb = "wphq_wp_eMember_members_tbl";
	
//	$user = $_POST['item_meta']['741'];
	$email = $_POST['item_meta']['772'];
	
	$check = mysqli_query($dbc,"SELECT * FROM $memberdb WHERE email = '$email'");
	$rowcount = mysqli_num_rows($check);
	if ($rowcount == 0) {  // email has not been found - error
		 $errors['my_error'] = 'This email does not exist.';
	     return $errors;
		 }
  }
}
Rob LeVineRob LeVine replied 2 years ago

My guess is that the other two functions don't return $errors as a default, so when the code drops through, nothing is returned. My suggestion is to put them all in one function, like this https://paste2.org/vyDHvLfP and that will make debugging a lot easier. And make sure to always return $errors.

Roger WeissRoger Weiss answered 2 years ago
Thank you. Good suggestion...that seemed to do it. Staging site working fine, Now I'll move to Live site. Thanks again, Roger
Roger WeissRoger Weiss answered 2 years ago
Big Thank You to Rob for his invaluble help in diagnosing and fixing this issue. I'm very glad we have this forum since the Formidable Support team offers very limited support. When I get a chance, I will probably rewrite my other snippets similarly to this new one, thus avoiding future "conflicts".
Bobby Clapp replied 2 years ago

And if you are comfortable in doing so, write up a post in the appropriate forum sharing your code and what it does such that it might help someone else.

Roger WeissRoger Weiss answered 2 years ago
Not sure where else to post this, but Rob sent me a perfect example:
  1. add_filter('frm_validate_entry', 'my_one_badass_validation', 20, 2);
  2. function my_one_badass_validation($errors, $values) {
  3. switch ($values['form_id']) {
  4. case 6: // validate username in form 6
  5. // do stuff
  6. break;
  7. case 7: // validate subscription in form 7
  8. // do stuff
  9. break;
  10. case 27: // validate email in form 27
  11. // do stuff
  12. break;
  13. }
  14.  
  15. return $errors;
  16. }

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right