Last active
May 18, 2020 02:54
-
-
Save spivurno/3710917 to your computer and use it in GitHub Desktop.
Gravity Wiz // Double Confirmation Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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