Conditional form field validation

By: Elizabeth Martinez | Asked: 08/03/2023
ForumsCategory: Code HelpConditional form field validation
Elizabeth Martinez asked 8 months ago

I have a form that is meant to allow a logged in user to select different certifications that they have, but in order to avoid extra administrative work, there is a registration code for each certification. So there are 7 different certifications and 7 different text fields for a registration code.  But, the registration code field doesn't show up unless someone has selected "yes" next to the appropriate certification.  However, when I added the form validation code to the functions.php it won't let the form be submitted unless the right code is in each field being validated.  Is there a way to make the form validation conditional? I know just enough PHP to get myself in trouble sometimes, but not always enough to edit everything.    https://dognosticseducation.com/test-certification-add-form/  - the form is embedded on that page as a test.  When I had the validation in, I was using: 

add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 3); function my_custom_validation($errors, $posted_field, $posted_value){ if($posted_field->id == 46){ //change 25 to the ID of the field to validate if(!in_array($posted_value, array('DOGTRAINPRO','dogtrainpro'))){ //change 001 and 002 to your allowed values //if it doesn't match up, add an error: $errors['field'. $posted_field->id] = 'Dog Training Professional registration code is wrong!'; } } return $errors; }  

add_filter('frm_validate_field_entry', 'my_custom_validation1', 10, 3); function my_custom_validation1($errors, $posted_field, $posted_value){ if($posted_field->id == 47){ //change 25 to the ID of the field to validate if(!in_array($posted_value, array('DOGBEHPRO','dogbehpro'))){ //change 001 and 002 to your allowed values //if it doesn't match up, add an error: $errors['field'. $posted_field->id] = 'Dog Behavior Professional registration code is wrong!'; } } return $errors; }  

add_filter('frm_validate_field_entry', 'my_custom_validation2', 10, 3); function my_custom_validation2($errors, $posted_field, $posted_value){ if($posted_field->id == 48){ //change 25 to the ID of the field to validate if(!in_array($posted_value, array('CETPRO','cetpro'))){ //change 001 and 002 to your allowed values //if it doesn't match up, add an error: $errors['field'. $posted_field->id] = 'Certified Canine Enrichment Technician registration code is wrong!'; } } return $errors; }    

add_filter('frm_validate_field_entry', 'my_custom_validation3', 10, 3); function my_custom_validation3($errors, $posted_field, $posted_value){ if($posted_field->id == 49){ //change 25 to the ID of the field to validate if(!in_array($posted_value, array('DOGBITEPRE','dogbitepre'))){ //change 001 and 002 to your allowed values //if it doesn't match up, add an error: $errors['field'. $posted_field->id] = 'Dog Bite Prevention Educator registration code is wrong!'; } } return $errors; }  

add_filter('frm_validate_field_entry', 'my_custom_validation4', 10, 3); function my_custom_validation4($errors, $posted_field, $posted_value){ if($posted_field->id == 50){ //change 25 to the ID of the field to validate if(!in_array($posted_value, array('FUNSCENTINST','funscentinst'))){ //change 001 and 002 to your allowed values //if it doesn't match up, add an error: $errors['field'. $posted_field->id] = 'Fun Scent Games Instructor registration code is wrong!'; } } return $errors; }  

add_filter('frm_validate_field_entry', 'my_custom_validation5', 10, 3); function my_custom_validation5($errors, $posted_field, $posted_value){ if($posted_field->id == 51){ //change 25 to the ID of the field to validate if(!in_array($posted_value, array('PETCAREPRO','petcarepro'))){ //change 001 and 002 to your allowed values //if it doesn't match up, add an error: $errors['field'. $posted_field->id] = 'Pet Care Professional registration code is wrong!'; } } return $errors; }  

add_filter('frm_validate_field_entry', 'my_custom_validation6', 10, 3); function my_custom_validation6($errors, $posted_field, $posted_value){ if($posted_field->id == 52){ //change 25 to the ID of the field to validate if(!in_array($posted_value, array('DOGWALKPRO','dogwalkpro'))){ //change 001 and 002 to your allowed values //if it doesn't match up, add an error: $errors['field'. $posted_field->id] = 'Pet Care Professional registration code is wrong!'; } } return $errors; }  

 

 

If I keep all of those in the functions.php as is, the form won't submit unless the right code is in each registration code field - but, someone might only need to add one or two certifications.   If someone could point me in the right direction, I would greatly appreciate it.  

Elizabeth Martinez replied 8 months ago

I need to make the validations conditional

1 Answers
Victor Font answered 8 months ago
You've got a lot of code in this post that makes the post very difficult to read. Please use pastebin or a gist for code and just paste the link to the code inn your question. I don't even know what you're asking because I didn't read it. It's too complicated with the code in it.

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