NinjaForms服务器端验证冻结在“处理"状态, [英] NinjaForms Server Side Validation Frozen on "Processing"

查看:52
本文介绍了NinjaForms服务器端验证冻结在“处理"状态,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Wordpress 5.1.1上使用Ninja Forms提交一个简单的表单.但是,我需要进行简单的服务器端验证.我已经通过文档和网络进行了数天的筛选,但是找不到解决此问题的解决方案.

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.

到目前为止,我已经能够将我的功能附加到Ninja Form的 ninja_forms_submit_data Webhook上.我已经确认PHP确实正在执行.但是,当我提交表单时,当我尝试返回自定义错误时,它停留在正在处理"中.我相信这与AJAX响应格式有关,但我不确定.甚至可能是网站安全阻止了该请求,但我对此表示怀疑.我认为,更有可能的是,我对NinjaForms在后端的运行方式缺乏根本的误解.

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.

我的背景主要是C/C ++,Java,Python和Ruby.PHP让我有些失望.您能提供的任何帮助都将受到赞赏.

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.

这是在我的过滤器中调用的验证检查.无论我是否将 $ errors 设置为 [] ,提交都将在处理时冻结.仅在执行 if(!validateSite($ form_data,$ field_id))块时才会发生.否则,表格提交就可以了.只有在没有错误要报告的情况下,Processing Freeze才会冻结.

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
}

这是执行的过滤器:

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;
});

推荐答案

搜索了一段时间之后,我设法找到了自己的答案.考虑到缺少有关此主题的详细文档,我相信有人可能会觉得有帮助.下面的代码有效地工作.链接有很大帮助.

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;
});

这篇关于NinjaForms服务器端验证冻结在“处理"状态,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆