NinjaForms Server Side Validation Frozen on “Processing”

情到浓时终转凉″ 提交于 2021-02-19 23:37:45

问题


I'm working with Ninja Forms on Wordpress 5.1.1 to submit a simple form. However, I need a simple server-side validation to take place. I've been sifting for days through documentation and the web, but I cannot find a solution to this issue.

So far, I've been able to attach my function to the Ninja Form's ninja_forms_submit_data webhook. I've confirmed that the PHP is indeed being executed. However, when I submit the form, it is stuck on "Processing" when I try to return a custom error. I believe this has something to do with the AJAX response format, but I can't be certain. It might even be website security blocking the request, but I doubt it. More likely, in my opinion, it is my fundamental lack of misunderstanding of how NinjaForms operates on the backend.

At this point, I'd even be willing to do client-side validation (The security in the context isn't crucial). But I'm not even sure how that would be accomplished.

My background is mostly in C/C++, Java, Python, and Ruby. PHP is throwing me off a bit. Any help you can provide is appreciated.

This is the Validation check called in my filter. Regardless if I set $errors to [], the submit will freeze on processing. This ONLY occurs if the if( !validateSite( $form_data, $field_id ) ) block is executed. Otherwise, the form submits perfectly fine. It's only if there are no errors to be reported that Processing Freezes.

function validateSite($form_data, $field_id){
    // $site_code = $form_data['fields'][$field_id]['value'];

    $form_fields   =  $form_data[ 'fields' ];
    foreach( $form_fields as $field ) 
    {
        $field_id    = $field[ 'id' ];    
        $field_key   = $field[ 'key' ];
        $field_value = $field[ 'value' ];

        // Example Field Key comparison
        if( "site_code_1552245398425" == $field_key ) 
        //I Strongly Suspect that this site code is never found,
        //as I can never get this method to return false without
        //hard-setting the default return. (Regardless whether or
        //not the input is even or odd.)
        {
            if(intval($field_value) % 2 != 0) //EXAMPLE TEST
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }  
    return false; //hard set to false
}

This is the filter that is executed:

add_filter( 'ninja_forms_submit_data', function( $form_data ){
  $field_id = 'nf-field-21';

  if( !validateSite( $form_data, $field_id ) ) 
  {

    $errors = [];
    $form_fields   =  $form_data[ 'fields' ];
    foreach( $form_fields as $field ) //iterate through the fields
    {
        $field_id    = $field[ 'id' ];
        $field_key   = $field[ 'key' ];
        $field_value = $field[ 'value' ];

        if($field_key == "site_code_1552245398425")
        {
            //$errors = ['fields'][$field_id] = "INVALID SITE CODE";
            $errors = ['fields' => [$field_id => "INVALID SITE CODE"]];
        }
    }

    /*
    $errors = [ 'fields' => [ 'nf-field-21' => __( 'Invalid Site Code.', 'my-plugin' ) ],];
    */
    $response = [
        'errors' => $errors,
    ];
    echo wp_json_encode( $response );
    wp_die(); // this is required to terminate immediately and return a proper response
  }
  // If no errors, be sure to return the $form_data.
    return $form_data;
});

回答1:


After searching a little longer, I managed to actually find my own answer. Considering the lack of detailed documentation on this topic, I believe that someone may find this helpful. The code below works effectively. THIS link helped a great deal.

function validateSite($form_data, $fieldID)
{
    if(intval($form_data['fields'][$fieldID]['value']) % 31 == 0)
    {
        return true; //No errors
    }
    else
    {
        return false; //ERROR!
    }
}

add_filter( 'ninja_forms_submit_data', function( $form_data ){
  $field_id = 21; //Field ID is NUMERICAL and not a string like 'nf-field-21', despite what you see in the front-end html.

  if( !validateSite( $form_data, $field_id ) ) 
  {
      //This is the EASY way to set an error on a field.
      $form_data['errors']['fields'][21] = "INVALID SITE CODE";
  }

  // If no errors, be sure to return the $form_data.
    return $form_data;
});


来源:https://stackoverflow.com/questions/55443553/ninjaforms-server-side-validation-frozen-on-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!