Forked from spivurno/gw-gravity-forms-value-exists-validation.php
Created
March 11, 2016 03:51
-
-
Save prayas-sapkota/8e3ef73b92ce70c9378b to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Validate that a Value Exists
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 // Validate that a Value Exists | |
* | |
* Ensure that a value entered in Form A has been previously submitted on Form B. This is useful if you're generating a reference number of some sort | |
* on Form B and would like the user to enter it on Form A. | |
* | |
* @version 1.2 | |
* @author David Smith <david@gravitywiz.com> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/... | |
*/ | |
class GW_Value_Exists_Validation { | |
public function __construct( $args = array() ) { | |
// set our default arguments, parse against the provided arguments, and store for use throughout the class | |
$this->_args = wp_parse_args( $args, array( | |
'target_form_id' => false, | |
'target_field_id' => false, | |
'source_form_id' => false, | |
'source_field_id' => false, | |
'validation_message' => __( 'Please enter a valid value.' ), | |
'field_map' => array(), | |
) ); | |
// do version check in the init to make sure if GF is going to be loaded, it is already loaded | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
// make sure we're running the required minimum version of Gravity Forms | |
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) { | |
return; | |
} | |
add_filter( 'gform_validation', array( $this, 'validation' ) ); | |
} | |
public function validation( $result ) { | |
if( ! $this->is_applicable_form( $result['form'] ) ) { | |
return $result; | |
} | |
foreach( $result['form']['fields'] as &$field ) { | |
if( $this->is_applicable_field( $field ) && ! GFFormsModel::is_field_hidden( $result['form'], $field, array() ) ) { | |
if( ! $this->do_values_exists( $this->get_values(), $this->_args['source_form_id'] ) ) { | |
$field['failed_validation'] = true; | |
$field['validation_message'] = $this->_args['validation_message']; | |
$result['is_valid'] = false; | |
} | |
} | |
} | |
return $result; | |
} | |
function does_value_exist( $value, $form_id, $field_id ) { | |
$entries = GFAPI::get_entries( $form_id, array( 'status' => 'active', 'field_filters' => array( array( 'key' => $field_id, 'value' => $value ) ) ) ); | |
return count( $entries ) > 0; | |
} | |
function do_values_exists( $values, $form_id ) { | |
$field_filters = array(); | |
foreach( $values as $field_id => $value ) { | |
$field_filters[] = array( | |
'key' => $field_id, | |
'value' => $value | |
); | |
} | |
$entries = GFAPI::get_entries( $form_id, array( 'status' => 'active', 'field_filters' => $field_filters ) ); | |
return count( $entries ) > 0; | |
} | |
function get_field_map() { | |
if( ! empty( $this->_args['field_map'] ) ) { | |
$field_map = $this->_args['field_map']; | |
} else { | |
$field_map = array( $this->_args['target_field_id'] => $this->_args['source_field_id'] ); | |
} | |
return $field_map; | |
} | |
function get_values() { | |
$field_map = $this->get_field_map(); | |
$values = array(); | |
foreach( $field_map as $target_field_id => $source_field_id ) { | |
$value = rgpost( 'input_' . $target_field_id ); | |
if( $value ) { | |
$values[ $source_field_id ] = $value; | |
} | |
} | |
return $values; | |
} | |
function is_applicable_form( $form ) { | |
$form_id = isset( $form['id'] ) ? $form['id'] : $form; | |
return $form_id == $this->_args['target_form_id']; | |
} | |
function is_applicable_field( $field ) { | |
$field_map = $this->get_field_map(); | |
$target_field_ids = array_keys( $field_map ); | |
return $this->is_applicable_form( $field->formId ) && in_array( $field->id, $target_field_ids ); | |
} | |
} | |
# Configuration | |
new GW_Value_Exists_Validation( array( | |
'target_form_id' => 613, | |
'target_field_id' => 1, | |
'source_form_id' => 519, | |
'source_field_id' => 1, | |
'validation_message' => 'Hey! This isn\'t a valid reference number.' | |
) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment