Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spivurno/3710917 to your computer and use it in GitHub Desktop.
Save spivurno/3710917 to your computer and use it in GitHub Desktop.
Gravity Wiz // Double Confirmation Fields
<?php
/**
* Gravity Wiz // Gravity Forms // Double Confirmation Fields
*
* Require a field's value to be entered twice to confirm it.
*
* @version 0.2
* @author David Smith <[email protected]>
* @license GPL-2.0+
* @link http://gravitywiz.com/custom-field-confirmation/
*
* Plugin Name: Snippet Name
* Plugin URI: http://gravitywiz.com/custom-field-confirmation/
* Description: Require a field's value to be entered twice to confirm it.
* Author: Gravity Wiz
* Version: 0.2
* Author URI: http://gravitywiz.com
*/
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 $confirm_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;
$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) {
global $gfcf_fields;
if(!$gfcf_fields)
$gfcf_fields = array();
if(!isset($gfcf_fields[$form_id]))
$gfcf_fields[$form_id] = array();
$gfcf_fields[$form_id][] = $fields;
}
register_confirmation_fields( 1, array( 2, 3 ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment