首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gravityforms和验证函数

Gravityforms和验证函数
EN

Stack Overflow用户
提问于 2014-06-24 16:15:48
回答 1查看 1.2K关注 0票数 0

我使用多页重力表单。下面的代码检查第一页,我不能点击next按钮,因为字段在第二页,它是无效的。我试过使用$current_page == 2,但它对我不起作用,我也不知道为什么。

下面代码的平和。如果有人能帮我一把就太好了。

代码语言:javascript
复制
// validate 9 digit code
// change here to your form ID
add_filter('gform_validation_1', 'validate_code');
function validate_code($validation_result){
        // this assumes the code is entered in field one on your form
        // change this input_ number if it's a different field
        if(!is_code_valid($_POST['input_18'])) {

        $validation_result['is_valid'] = false;
        foreach($validation_result['form']['fields'] as &$field){
            // field 1 is the field where we want to show the validation message
            if($field['id'] == 18){
                $field['failed_validation'] = true;
                                $field['validation_message'] = 'The code you entered is invalid: please try again.';
                break;
            }
        }
    }
    return $validation_result;
}

// use this function to validate codes
function is_code_valid($thiscode){
        // read all the codes in from the numbers.txt file
        // change the path here to the location of your file
        $codes = file('/htdocs/homepages/99/numbers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach($codes as $code){
                // compare the entered code to all codes in the file until we find a match
                if($thiscode == $code){
                        return TRUE;
                }
        }
        // if we did not have a match and are out of codes, return FALSE
        return FALSE;
}

// doing this here because the redirect URL does not support variables or shortcodes
// change the 70 here to your form ID
add_filter('gform_confirmation_1', 'valid_invitation_confirmation', 10, 4);
function valid_invitation_confirmation($confirmation, $form, $lead, $ajax){
        // customize this URL - I send the code in the query string
        $success_url = get_bloginfo('url') . '/?code=' . $lead[1];
        $confirmation = array('redirect' => $success_url);
        return $confirmation;
}
EN

回答 1

Stack Overflow用户

发布于 2014-06-25 04:34:40

正如您已经意识到的,您只想在包含该字段的表单页面提交时验证该字段。以下是如何检查的方法:

代码语言:javascript
复制
if( $field['pageNumber'] == GFFormDisplay::get_source_page($form['id'] ) ) {
    // run your custom validation here
}

这将进入$fields foreach循环中。您可以在one of my snippets上看到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24381560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档