我在表单的第 1 页上有一个字段 47,在第 3 页上有一个字段 55。如何在第 3 页上创建一个gform_validation,以便在字段 47
谢谢!
// multi-page validation
add_filter( 'gform_validation', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result['form'];
// Financial Assets must be larger than Investment
if ( rgpost( 'input_47' ) < rgpost( 'input_55' ) ) {
// set the form validation to false
$validation_result['is_valid'] = false;
//finding Field with ID of 1 and marking it as failed validation
foreach( $form['fields'] as &$field ) {
//NOTE: replace 1 with the field you would like to validate
if ( $field->id == '34' ) {
$field->failed_validation = true;
$field->validation_message = 'Your Financial Assets (A) cannot be lower than your investment with us. Please fix the discrepancy.';
break;
}
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
我在他们的文档中找到了以下信息,但我不确定如何在上面的代码中实现它。有什么线索吗?多谢!
// 3 - Get the current page being validated
$current_page = rgpost( 'gform_source_page_number_' . $form['id'] ) ? rgpost( 'gform_source_page_number_' . $form['id'] ) : 1;
首先,您可以通过GFFormDisplay::get_source_page( $form_id )
方法获取提交的页面。
有几种不同的方法可以做到这一点。为简单起见,我会绕过验证,直到提交您想要的页面。为此,只需添加如下内容:
if( GFFormDisplay::get_source_page( $form_id ) >= 3 ) {
return $validation_result;
}
。在此行之后:
$form = $validation_result['form'];
这只会在大于第 3 页的页面上运行此验证。