1

I'm having a problem with custom form field validation. I have installed Ninja form 3 in the latest wp-install. The problem is that I am not clear on which hook to use. I have looked at the documentation on their site and tried several thing but no results.

Here's what I'm trying to do: there is a field in the form where the user must give his member-id that value. I want to compare with data from a second database before the form is submit and when the id does not match throw an error-message.

I have tried the filter ninja_forms_submit_data but that gives error messages if I'm using get_field_value method. Can anyone help me in the right direction. Below the code that i have tried. Thanks in advance. M

add_filter( 'ninja_forms_submit_data', 'form_field_verification_pre_process');

function  form_field_verification_pre_process( $form_data){


$mydb = new wpdb('****','****','****','****');
$rows = $mydb->get_results("select title from ****_content");

$row1 = array_column($rows, 'title');


foreach(array_values($row1) as $key) :
$row1[$key] = 1;

endforeach;


foreach( $form_data[ 'fields' ] as $field ) { // Field settigns, including the field key and value.

if( 'id_vispas_nummer' != $field[ 'key' ] ) continue; // Check the field key to see if this is the field that I need to update.

$user_value = $field[ 'value' ] ; // Update the submitted field value.
}

if(isset($row1[$user_value]) ){


$ninja_forms_processing->add_error('id_vispas_nummer', 'Vispasnummer komt niet voor in  ons ledenbestand', 'id_vispas_nummer');


}
return $form_data;

}

Also tried to use the Hook below

add_action( 'ninja_forms_after_submission', 'form_field_verification_pre_process');

function  form_field_verification_pre_process( $form_data){
global $ninja_forms_processing;


$mydb = new wpdb('****','****','****','****');
$rows = $mydb->get_results("select title from ****_content");

$row1 = array_column($rows, 'title');


foreach(array_values($row1) as $key) :
$row1[$key] = 1;

endforeach;

foreach( $form_data[ 'fields' ] as $field ) { // Field settigns, including the field key and value.

if( 'id_vispas_nummer' != $field[ 'key' ] ) continue; // Check the field key to see if this is the field that I need to update.

$user_value = $field[ 'value' ] ; // Update the submitted field value.
}

if(isset($row1[$user_value]) ){


$ninja_forms_processing->add_error('id_vispas_nummer', 'Vispasnummer komt niet voor in  ons ledenbestand', 'id_vispas_nummer');



}


}
  • what is this code? what kind of errors do you get? – Mark Kaplun Sep 19 '17 at 12:36
  • @MarkKaplun I'am testing a form validation the first peace of code gives me no errors and send the form also when the user_value is not in the $row1 array. – Dpi-Art Sep 19 '17 at 15:28
  • @MarkKaplun The second peace of code i tried a different hook but that gives me an error. Uncaught Error: Call to a member function add_error() on null in /wp-content/themes/thefox_child_theme/functions.php:113 I have read the some docs on the ninja dev site but i can't get it clear on wich hook to use for this custom validation. I hope someone can give me the wright angle on this – Dpi-Art Sep 19 '17 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.