Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tweaked to allow a custom message
<?php
/**
* Double Confirmation Fields
* http://gravitywiz.com/2012/05/01/custom-field-confirmation/
*/
register_confirmation_fields( 10, array( 1, 2 ), 'Your custom messages do not match.' );
add_filter( 'gform_validation', 'gfcf_validation' );
function gfcf_validation( $validation_result ) {
global $gfcf_fields;
$form = $validation_result[ 'form' ];
$confirm_error = false;
if ( !isset( $gfcf_fields[ $form[ 'id' ] ] ) )
return $validation_result;
foreach ( $gfcf_fields[ $form[ 'id' ] ] as $fields ) {
$message = '';
$message = $fields[ 'message' ];
$confirm_fields = $fields[ 'fields' ];
$values = array();
// loop through form fields and gather all field values for current set of confirm fields
foreach ( $form[ 'fields' ] as $field ) {
if ( !in_array( $field[ 'id' ], $confirm_fields ) )
continue;
$values[] = rgpost( "input_{$field['id']}" );
}
// filter out unique values, if greater than 1, a value was different
if ( count( array_unique( $values ) ) <= 1 )
continue;
$confirm_error = true;
foreach ( $form[ 'fields' ] as &$field ) {
if ( !in_array( $field[ 'id' ], $confirm_fields ) )
continue;
// fix to remove phone format instruction
if ( RGFormsModel::get_input_type( $field ) == 'phone' )
$field[ 'phoneFormat' ] = '';
$field[ 'failed_validation' ] = true;
if ( $message ) {
$field[ 'validation_message' ] = $message;
} else {
$field[ 'validation_message' ] = 'Your values do not match.';
}
}
}
$validation_result[ 'form' ] = $form;
$validation_result[ 'is_valid' ] = !$validation_result[ 'is_valid' ] ? false : !$confirm_error;
return $validation_result;
}
function register_confirmation_fields( $form_id, $fields, $message ) {
global $gfcf_fields;
if ( !$gfcf_fields )
$gfcf_fields = array();
if ( !isset( $gfcf_fields[ $form_id ] ) )
$gfcf_fields[ $form_id ] = array();
$gfcf_fields[ $form_id ][] = array(
'fields' => $fields,
'message' => $message
);
}
@NightWidow

This comment has been minimized.

Copy link

NightWidow commented Feb 8, 2016

Hi Nick, I'm trying to use this snippet, because it does exactly were i'm looking for. But when I add this snippet to my functions.php and change the custom message, my website keeps crashing and gives me a blank screen. Do you know why this happens?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.