Search code examples
wordpressformssurvey

check if user email exists, and if not, stop the submit of the ninja form


I've one form built with ninja forms, and I use ajax to send it. I need to check if the email introduced already exists in database (user_email), and if it exists properly, I send the form properly, but if it doesn't exist, the form isn't submitted, and I need to give the user the message like "email does not exist". The form is a survey to be completed by a registered user, who gives us a feedback about our services, but the survey is located in a page where the user can send its opinion without needed to be logged.

I'm investigating, and at the moment I have:

function example_disable_saving_subs( $save, $form_id ) {

    global $ninja_forms_processing;
    $form_id = $ninja_forms_processing->get_form_ID();
    $email = ninja_forms_get_field_by_id( 18 );

    //cuestionario feedback profesor sobre creación de un curso
    if($form_id == 3){
        if( !email_exists( $email )) {
            $save = false;
            $ninja_forms_processing->add_error('email_no_existe', 'El email no existe');
        }
    }
    return $save;
}
add_filter( 'ninja_forms_save_submission', 'example_disable_saving_subs', 2, 10 );

But I pick up the field $email without value introduced...In addition, I don't know the way to give the user the message "email does not exists".

As you see, I chose the filter ninja_forms_save_submission. Maybe this is not the correct filter. I hope your valious help. Thanks in advance, Daniel


Solution

  • thanks for your help @Renato , I give you +1 :) It's true that I can do it through the way you tell me, but I don't want to break the api of WordPress, that is, the way this cms uses javascript, php, etc etc...So, I wanted to do this through the API of ninja forms, which is the plugin I use for build this survey. Finally, I solved it...it was my mistake, because I didn't use the correct filter...Investigating few more, there's another filter much more appropiate: ninja_forms_pre_process Here is the code:

    function add_change_ninja_forms_landing_page(){
        add_action( 'ninja_forms_pre_process', 'handle_custom_ninja_forms' );
    }
    add_action( 'init', 'add_change_ninja_forms_landing_page' );
    
    function handle_custom_ninja_forms(){
    
        global $ninja_forms_processing;
    
        $form_id = $ninja_forms_processing->get_form_ID();  
    
        //if it's my form of survey
        if( $form_id == 3 ){
    
            $email = $ninja_forms_processing->get_field_value( 18 ); //pick up the value of the email field    
           //use the native function of wordpress to check if there's a user with this email 
           //is anyone has this email, it does not exist   
           if( !email_exists( $email )) {
                    $ninja_forms_processing->add_error('email_no_existe', 'El email indicado no está registrado en nuestra base de datos'); //add_error stop the form and gives the error message                
            }
        }
    }
    

    With the code above everything works fine! :) Thanks!